in reply to pattern matching

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.

Replies are listed 'Best First'.
Re^2: pattern matching
by gwadej (Chaplain) on Apr 10, 2009 at 14:20 UTC

    Although I don't disagree with the overall thrust of your argument, there is a tiny mistake.

    However.. if your text chunk is:
    $text='ADABCDE6789023424'; $text=~/([A-Z]{2,5}\d{5})/ or die; print $1; # will print 'DE67890'

    It actually would print ABCDE67890. Remember the {2,5} matches the longest string it can, unless you make it non-greedy.

    G. Wade