in reply to matching problem

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,

Replies are listed 'Best First'.
Re^2: matching problem
by Anonymous Monk on Dec 27, 2018 at 10:04 UTC
    Socratic not spoon :)