Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Regular expression quantifiers and the /gsmx modifiers

by morgon (Curate)
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.


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.

Replies are listed 'Best First'.
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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://976602]
[erix]: *chuckle*
[marinersk]: karlgothebier I am glad to see you are still here. Have you decided on retirement or to continue the job hunt?

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2017-05-25 15:50 GMT
Find Nodes?
    Voting Booth?