Your match isnt anchored.

There are 3 character classes so on someRandomName- it could successfully match like so. The first character class matches 's' the second matches 'o' and the third matches 'm' so you have a successful match

Since RE's are greedy whats really happening is that the first matches the whole thing except the - the second set matches the - the third fails. Then the RE backtracks and the first matches everything but 'e-' and the second matches e- and the third fails.

Then the first matches up to 'ame-' the the second matches 'ame-' the 3rd fails the second backtracks until it matches 'm' then the 3rd matches 'e' and SUCESS!

If you want to match at end of string you need to add a $ to the end or if you want to match words (my take on the question) you need to add \b to both ends like so:

if ($domain =~ /[A-Za-z0-9]+[A-Za-z0-9-]+[A-Za-z0-9]$/) #matches at en +d of text if ($domain =~ /\b[A-Za-z0-9]+[A-Za-z0-9-]+[A-Za-z0-9]\b/) #matches on +ly words seperate from non words.

Also I got rid of the /i which should be faster but as has been noted above you should use the \w type escapes where possible instead of the spelled out classes.

Your current code will also match my-do^%$%^@#@ which you don't seem to intend the \b will fix that since the word in question has stuff not in the classes and so it is a fail. In fact your RE will match any 3 legal (based on your RE) sequence in any text.

Hope this helps.


In reply to Re: Regular expression matching when it shouldn't by dga
in thread Regular expression matching when it shouldn't 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.