Greetings Monks!

I am new to perl & trying to do some regex codes. Now I need your help to proceed further. A full day now & I didn't find a solution yet for this.

Ok, here is it!

This code works,
$title =~ /\b$kw\b/g;

This code also works
$title =~ /\Q$kw/g;

How to make this both work in a single code ?
ie, something like this,
$title =~ /\Q\b$kw\b/g; (BTW, this code won't work for me!)

What I want to achieve,
1) Match words(& count them) in a string but don't check in the substings. (reason why I used \b)
2) To be able to escape certain regex characters (like +) and consider them when matching words (reason why I used \Q)

An example scenario,
When you give .NET as $kw, the count should be 3 and not 4
when you give C++, the count should be 0 instead of 2
When you give C, the count should be 1

my $kw = ".NET"; # For different scenarios pls use C (& then C++) instead of .NET for $ +kw my $title = "C .NET Cobol .NET .NET .NETER Perl"; my $count = () = $title =~ /$kw/ig; print $count; die;

Any help is greatly appreciated.

Thank you


In reply to Regex help \b & \Q 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.