Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • 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 goofing around in the Monastery: (4)
    As of 2025-06-23 15:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.