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: Re: [FishMonger] Regex on filenames: Edit Log



BillKSmith
Veteran

Feb 23, 2016, 3:29 PM


Views: 2079
Re: [FishMonger] Regex on filenames

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)


Edit Log:
Post edited by BillKSmith (Veteran) on Feb 24, 2016, 6:07 AM


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

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