I often match things like these into text chunks.
The question and answers here are great to validate or check a value, but what if you're fishing for these out of a text chunk?

For example.. if the text you are matching into is: $text = 'ABCDE67890';

Then, yes..

$text=~/([A-Z]{2,5}\d{5})/ or die; # parenthesis is for "remembering" what we matched, # we can get it with $1 later.. print "got $1";

However.. if your text chunk is:

$text='ADABCDE6789023424'; $text=~/([A-Z]{2,5}\d{5})/ or die; print $1;
# will print 'DE67890' ABCDE67890. Thanks gwadej (see below).

Then you still match into this. Is this the behaviour you want? I don't know of the context into whuch you are matching.. If it is possible that you want to check the *entire* string as *the* pattern.. You need to do this instead: $text=~/^([A-Z]{2,5}\d{5})$/ or die;
(Having the ^ means start at beginning, having $ marks the end.)

If you want to match into a large text chunk such as:

This YU123456 is a piece of text and the id code would be AG12345 orAH12345

Then this pattern will match AG12345 but NOT AH12345, and NOT YU12345: /\b[A-Z]{2}\d{5}\b/ And this pattern will match all of them: /[A-Z]{2}\d{5}/

Just something to keep in mind.


In reply to Re: pattern matching by leocharre
in thread pattern matching by Spooky

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.