Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Regular Expression ?

by AnomalousMonk (Chancellor)
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]
[Eily]: what? I thought PHP was known for its reliable and consistent behaviour and documentation!
[LanX]: ... and a lot of pseudo Perl experts
[Eily]: though honestly, I'm sure I have seen people use closure to mean anonysub for perl as well
[stonecolddevin]: good morning friends
[LanX]: yes, mainly pseudo Perl experts
[Eily]: good morning stonecolddevin
LanX has to go/
[Eily]: bye LanX
[LanX]: bye :)

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (15)
As of 2018-02-23 17:19 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (305 votes). Check out past polls.