Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

matching problem

by catfish1116 (Beadle)
on Dec 26, 2018 at 22:13 UTC ( [id://1227723]=perlquestion: print w/replies, xml ) Need Help??

catfish1116 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get a match on a line that begins with 'barney'. Below is the code. I have ran this and I always get the message, 'barney not found'. When I take the ^ and m away and just have /barney/, then it works.

$_ = 'This is a wilma line barney is on this line but this line ends with fred and a final dino line' ; if (/^barney/m) { print "Found barney \n"; } else { print "Barney not found \n"; } ~

Thanks in advance catfish

Replies are listed 'Best First'.
Re: matching problem
by jwkrahn (Abbot) on Dec 26, 2018 at 22:48 UTC

    The line with barney in it has whitespace at the beginning so /^barney/ won't match.

Re: matching problem
by syphilis (Archbishop) on Dec 26, 2018 at 22:54 UTC
    Hi,
    On the second line of $_, there's a number of whitespace characters that precede "barney" - hence that line does not begin with "barney":
    $_ = 'This is a wilma line barney is on this line but this line ends with fred and a final dino line' ; if (/^barney/m) { print "Found barney \n"; } else { print "Barney not found \n"; } $_ = 'This is a wilma line barney is on this line but this line ends with fred and a final dino line' ; if (/^barney/m) { print "Found barney \n"; } else { print "Barney not found \n"; } __END__ Outputs: Barney not found Found barney

    Cheers,
    Rob
Re: matching problem
by Athanasius (Archbishop) on Dec 27, 2018 at 03:54 UTC

    Hello catfish1116,

    Other monks have shown you where the problem lies, but (strangely) no-one has made the fix explicit: change the regular expression from /^barney/m to /^\s*barney/m. This says: match any line beginning with zero or more whitespace characters followed by “barney”.

    Perhaps it will make things a little clearer if we break the string into lines, and apply the regex to each line in turn:

    use strict; use warnings; my $para = 'This is a wilma line barney is on this line but this line ends with fred ** this line has barney in the middle ** and a final dino line'; my $count = 0; for my $line (split /\n/, $para) { printf "line %d: >%s< -- barney %sfound\n", ++$count, $line, $line =~ /^\s*barney/ ? '' : 'not '; }

    Output:

    13:49 >perl 1958_SoPW.pl line 1: >This is a wilma line< -- barney not found line 2: > barney is on this line< -- barney found line 3: > but this line ends with fred< -- barney not found line 4: > ** this line has barney in the middle **< -- barney +not found line 5: > and a final dino line< -- barney not found 13:49 >

    (Of course, when we know we’re dealing with a single line at a time, the /m modifier is no longer needed on the regex.)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Socratic not spoon :)
Re: matching problem
by beech (Parson) on Dec 26, 2018 at 22:49 UTC

    Hi

    There is no line that begins with 'barney'

    There is a line that begins with '         barney'

    https://metacpan.org/pod/re#%27debug%27-mode explains

    $ perl -Mre=debug barney Compiling REx "^barney" Final program: 1: MBOL (2) 2: EXACT <barney> (5) 5: END (0) anchored "barney" at 0 (checking anchored) anchored(MBOL) minlen 6 Guessing start of match in sv for REx "^barney" against "This is a wil +ma line%n barney is on this line%n ".. . Found anchored substr "barney" at offset 30... Found /^/m, restarting lookup for check-string at offset 53... Did not find anchored substr "barney"... Match rejected by optimizer Barney not found Freeing REx: "^barney"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1227723]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found