I have a test file (text.txt) with following lines.
<String> <This is the firstline> <This is the secondline>
What I want to do is to do a find the exact match & replace.
open (FH,"text.txt"); @lines=<FH>; @patterns = qw/<string> line secondline/; @replace = qw/'<REPLACED>' 'line_R' 'secondline_R'/; foreach $line (@lines){ chomp $line; for ($i=0;$i<=2;$i++){ if ($line =~ /\B$patterns[$i]\B/i){ $line =~ s/$patterns[$i]/$replace[$i]/i; push @repd,$line; } } } foreach $line (@repd){ print "$line\n"; }
My desired output.
'<REPLACED>' //<String>->'<REPLACED>' <This is the 'secondline_R'> // secondline->'secondline_R'
But what i am getting as output
With RegExp '/\B$patterns$i\B/i'

'<REPLACED>' //Match only non-word char boundary.

With RegExp "/\b$patterns$i\b/i"

<This is the 'secondline_R'> //failed to match non-word chars '<,>'.


Can anybody help fixing my REGEXP to exactly match word & non-word chars?
Thanks,

In reply to word & non-word boundries by Anonymous Monk

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.