Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: regexp mystery no xxx by davido
in thread regexp mystery no xxx by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (1)
As of 2024-04-25 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found