Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Regex help

by ww (Bishop)
on Sep 18, 2012 at 21:51 UTC ( #994373=note: print w/ replies, xml ) Need Help??


in reply to Regex help

The "$" at the end of your regex means 'match only if "R" followed by five digits' is at the END of your line of data. That said, there are numerous guesses already as to what your data might really look like (Hint: code tags!), so here's another. It may be relevant if your data looks like that in the code below:

#!/usr/bin/perl use 5.014; #994367 my @data = ( 'R00005: 00330: C01010 => C00011', 'R00005: 00791: C01010 => C00011', 'R00005: 01100: C01010 <=> C00011', 'R00006: 00770: C00022 => C00900', 'R00008: 00362: C06033 => C00022', 'R00008: 00660: C00022 => C06033', 'R00010: 00500: C01083 => C00031', 'R00013: 00630: C00048 => C01146', 'R00013: 01100: C00048 <=> C01146', ); for my $data (@data) { if ( $data =~ /(R\d{5}).+)/ ) { # Match on and capture any line +which # contains the sequence R, 5 dig +its, and # something more # The parens in the regex captur +e the # match to $1 (but are not used +here) # which could be used in other a +pplications. say "\t Match on data $data. Hooray!"; } else { say "No match on data $data"; } } # do it again for those beginning "R00005" ONLY say "\n\n doing it again for 'R00005' ONLY"; for my $data (@data) { if ( $data =~ /(R0{4})(5)(.+)/ ) { say "\t Match on data " . $1 . $2 . $3 . " Hooray!"; # 3 capt +ures, # print' +em # NOT a good pr +actice; # illustrate ON +LY one aspect # of regex capt +ures } else { say "No match on data $data"; } } =head output: Match on data R00005: 00330: C01010 => C00011. Hooray! Match on data R00005: 00791: C01010 => C00011. Hooray! Match on data R00005: 01100: C01010 <=> C00011. Hooray! Match on data R00006: 00770: C00022 => C00900. Hooray! Match on data R00008: 00362: C06033 => C00022. Hooray! Match on data R00008: 00660: C00022 => C06033. Hooray! Match on data R00010: 00500: C01083 => C00031. Hooray! Match on data R00013: 00630: C00048 => C01146. Hooray! Match on data R00013: 01100: C00048 <=> C01146. Hooray! doing it again for 'R00005' ONLY Match on data R00005: 00330: C01010 => C00011. Hooray! Match on data R00005: 00791: C01010 => C00011. Hooray! Match on data R00005: 01100: C01010 <=> C00011. Hooray! No match on data R00006: 00770: C00022 => C00900 No match on data R00008: 00362: C06033 => C00022 No match on data R00008: 00660: C00022 => C06033 No match on data R00010: 00500: C01083 => C00031 No match on data R00013: 00630: C00048 => C01146 No match on data R00013: 01100: C00048 <=> C01146 =cut

See perlretut.


Comment on Re: Regex help
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://994373]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-09-20 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls