Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: regexp mystery no xxx

by davido (Cardinal)
on Aug 18, 2003 at 21:33 UTC ( [id://284734] : note . print w/replies, xml ) Need Help??

in reply to regexp mystery no xxx

It is the /g modifier.

From the Camel book:

"In a scalar context, m//g iterates through the string, returning true for each time it matches, and false when it eventually runs out of matches. (In other words, it remembers where it left off last time and restarts the search at that point...)... ...If you modify the string in any way, the match position is reset to the beginning."

Take off the /g modifier and it will work properly. Let me suggest a couple of other improvements to the regexp you're using, by illustrating what I feel is a cleaner example of what you have (minus the /g, of course):

my $title = q(the 2000); if ($title =~/^the\s(\s*?)\d{4}$/i) { print "\ntrigger1\n" } if ($title =~/^the\s(\s*?)\d{4}$/i) { print "\ntrigger2\n" }

Note, in the example provided I put a \n at the end of each print, particularly because not all implementations on all operating systems will flush <STDOUT> when the program exits, so in *some* OS's, the final line might not print if it isn't terminated with a "\n". To get back to your original functionality just remove those two trailing \n's. Next, getting down to brass tacks. In the regexp's, I really prefer using something like \s when I mean whitespace, rather than leaving whitespace in the expression. It can be difficult to count through, when revising the code later, especially if several spaces are in a row. If \s doesn't give desired behavior, specify the character explicitly with \x20, or whatever.

The other issue is avoiding \d\d\d\d. It works but it's not as elegant as \d{4}.

Those are just opinions. The fact is that all you HAD to do to make your example work was remove the /g modifiers.

"If I had my life to do over again, I'd be a plumber." -- Albert Einstein