Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Regular Expression ?

by AnomalousMonk (Monsignor)
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.


Comment on Re: Regular Expression ?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2014-08-27 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (253 votes), past polls