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.
|