#. check the number of characters before the first "_" (underscore). #. If it's return only two characters, then get the string untill it reach the second "_"(underscore)
Okay, well because you are looking for anything that is not an underscore, then a negated character class is probably the way to go. Something like this:
if (/^([^_]{2}_[^_]+)_/) { print $1; }
else if more than 2 characters then it must be a targetted pattern
Sorry, but I don't get that. What is a "targetted pattern"?

I guess i can't able to use the "\w" (word) pattern, because it will take the whole string as a word.
Yes and no. It's okay, because you are limiting it to only the first two characters with {2}. But it's probably not okay because \w will also match an underscore. So \w{2} would match something like "_a", which I'm pretty sure you don't want.

Cheers,
Darren :)


In reply to Re^3: Regular Expression - pattern matching by McDarren
in thread Regular Expression - pattern matching by kulls

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.