CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Regex on filenames

 



wattsup
Novice

Feb 23, 2016, 1:53 PM

Post #1 of 6 (1974 views)
Regex on filenames Can't Post

I am trying to do a regex on the following strings. The only items in the string that will be in every filename is Sxxxx SC2550.csv. anything before "S" I don't care about, But the "S" will have 1 to 5 digits. The SC2550 will always be the same. Anyone see what my problem is??

filenames:

Code
Sheehan Majestic P99975 S14166 SC2550.csv  
WANG P YL63316 S142 SC2550.CSV
Wang PO YL63216 S14160 SC2550.csv


Trying:

Code
/\w+\s*(s\d{1+})\s(sc\d{4}).csv/i



FishMonger
Veteran / Moderator

Feb 23, 2016, 2:46 PM

Post #2 of 6 (1968 views)
Re: [wattsup] Regex on filenames [In reply to] Can't Post


Code
#!/usr/bin/perl 

use warnings;
use strict;

my @files = ('Sheehan Majestic P99975 S14166 SC2550.csv',
'WANG P YL63316 S142 SC2550.CSV',
'Wang PO YL63216 S14160 SC2550.csv');

foreach my $file (@files) {
if ($file =~ /(s\d+)\s(sc\d{4})\.csv$/i) {
print "$1\t$2$/";
}
}



Code
c:\test>Perl-1.pl 
S14166 SC2550
S142 SC2550
S14160 SC2550



(This post was edited by FishMonger on Feb 23, 2016, 2:48 PM)


BillKSmith
Veteran

Feb 23, 2016, 3:29 PM

Post #3 of 6 (1961 views)
Re: [FishMonger] Regex on filenames [In reply to] Can't Post

A slightly better regex requires whitespace before the 's' field and specifies the min and max number of digits for that field. The /x modifier allows the field specifications to be separated for clarity.

Code
use strict; 
use warnings;

#my $regex = qr/ \w+ \s* (s\d{1,5}) \s (sc\d{4}) \. csv /xi;
my $regex = qr/ \w+ \s+ (s\d{1,5}) \s (sc\d{4}) \. csv /xi;


my @examples = (
'Sheehan Majestic P99975 S14166 SC2550.csv',
'WANG P YL63316 S142 SC2550.CSV',
'Wang PO YL63216 S14160 SC2550.csv',
);

local $, = "\t";
print /$regex/, "\n" foreach @examples;



Code
C:\Users\Bill\perl\guru>wattsup.pl 
S14166 SC2550
S142 SC2550
S14160 SC2550


UPDATE: Corrected typo in regex as indicated by FishMonger.
Good Luck,
Bill

(This post was edited by BillKSmith on Feb 24, 2016, 6:07 AM)


FishMonger
Veteran / Moderator

Feb 23, 2016, 4:29 PM

Post #4 of 6 (1955 views)
Re: [BillKSmith] Regex on filenames [In reply to] Can't Post


Quote
A slightly better regex requires whitespace before the 's' field

I agree, but your regex doesn't require that whitespace. You need to change \s* to \s+


Laurent_R
Veteran / Moderator

Feb 23, 2016, 11:26 PM

Post #5 of 6 (1943 views)
Re: [wattsup] Regex on filenames [In reply to] Can't Post

Hi,
you've been given good solutions already, but just to let you know, your real error was in this part of the regex:


Code
s\d{1+}


You can have either s\d+, i.e. 's' followed by one or more digits, of s\d{1,}, meaning exactly the same, or s\d{1,4}, where the number of digits can be anything between 1 and 4.


wattsup
Novice

Feb 24, 2016, 2:19 PM

Post #6 of 6 (1934 views)
Re: [Laurent_R] Regex on filenames [In reply to] Can't Post

Thank You everyone!! With your help I have fixed the problem and working just fine!!!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives