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,


In reply to Re: matching problem by Athanasius
in thread matching problem by catfish1116

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.