Why did you add the '?'s ? They actually mean that the dot may be ignored so of course your regex doesn't do what you want. And even without that, by default regular expressions in perl search for a given pattern anywhere in the string. You need to add the token ^ or \A (see perlre and perlreref) to only match from the beginning of the line or string.

Anyway, in this case regular expressions are probably not the right tool, substr seems more appropriate and easy to use.

BTW, grep selects items in a list, $strTest is a single item, so grep is not needed here.

Edit: added "probably" in "regular expressions are not the right tool". Although it looks to me that it would be far better for the op to use substr because it does not have all the side effects, hidden rules and modifiers of regexes, I don't know for sure that the fifth char is the only thing that need to be checked.


In reply to Re: Perl RegEx doubt by Eily
in thread Perl RegEx doubt by swissknife

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.