Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Regular expression quantifiers and the /gsmx modifiers

by morgon (Deacon)
on Jun 16, 2012 at 22:03 UTC ( #976602=note: print w/ replies, xml ) Need Help??


in reply to Regular expression quantifiers and the /gsmx modifiers

I think the problem is that $ matches BEFORE a newline and ^ matches AFTER a newline (when using /sm), but you also need to consume the newline itself.

Consider:

use strict; my $s = <<"__end__"; hubba bubba __end__ print "matched 1\n" if $s =~ m/^hubba$ ^bubba$/smx; print "matched 2\n" if $s =~ m/^hubba$ \n ^bubba$/smx;
Here only the second regex matches because in the first after the $ matches there is still a newline left and therefore the ^ does not match. You need to consume that in the regex as the second example shows.


Comment on Re: Regular expression quantifiers and the /gsmx modifiers
Select or Download Code
Re^2: Regular expression quantifiers and the /gsmx modifiers
by davis (Vicar) on Jun 16, 2012 at 22:17 UTC

    Damn. I completely missed that.

    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $vg_details = ' --- Physical volumes --- PV Name /dev/dsk/c14t3d1 PV Name /dev/dsk/c15t3d1 Alternate Link PV Status available Total PE 15997 Free PE 0 Autoswitch On Proactive Polling On '; while($vg_details =~ m/^\s*PV\s+Name\s*(?<pv_name>\S+)\s*$ \n (^\s*PV\s+Name\s+(?<alt_link>\S+)\s+Alternate\s+Link\s*$ \n){0,20} + # skip them ^\s*PV\s+Status\s+(?<pv_status>\S+)\s*$ \n ^\s*Total\s+PE\s+(?<total_pe>\S+)\s*$ \n ^\s*Free\s+PE\s+(?<free_pe>\d+)\s*$ \n ^\s*Autoswitch\s+(?<autoswitch>\S+)\s*$ \n ^\s*Proactive\s+Polling\s+(?<proactive_polling>\S+)\s*$ \n/gsmx) { my $pv_name = $+{pv_name}; print "matched $pv_name"; }

    Seems to DWIM. I genuinely cannot believe I've been gawping at that for so long. My thanks.


    davis

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (19)
As of 2014-10-21 17:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (106 votes), past polls