Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Regular Expression ?

by AnomalousMonk (Canon)
on Apr 21, 2014 at 20:16 UTC ( #1083079=note: print w/replies, xml ) Need Help??

in reply to Regular Expression ?

Ok, couldn't resist, here's some code (needs Perl version 5.10+ for \K):

c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; use Regexp::Common qw(number); ;; my @lines = ( qq{Line 1 = 0154.45 +02 13.56 +03 64.35 \n}, qq{Line 2 = +09 23.12 +10 8798 \n}, ); ;; my $prefix = qr{ \G (?: \A Line \s* \d+ \s* =)? \s+ }xms; for my $line (@lines) { my @numbers = $line =~ m{ $prefix \K $RE{num}{real} }xmsg; printf qq{'$_' } for @numbers; print ''; } " '0154.45' '+02' '13.56' '+03' '64.35' '+09' '23.12' '+10' '8798'

Update: Of course,  \K isn't critical to this approach — I just like it so much... The expression
    m{ $prefix  ($RE{num}{real}) }xmsg
(\K removed, parentheses  (...) around the  $RE{...} sub-expression) produces identical output for the given input, and I'm not aware of any version incompatibilities.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2016-10-20 21:52 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (283 votes). Check out past polls.