Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://284734]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-03-28 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found