QM has asked for the wisdom of the Perl Monks concerning the following question:

Just got bit today (at least for a while) by this:
# prints "xxx_", instead of "xxx" print '__xxx__' =~ /\b_+(\w+)_+\b/ ? "$1\n" : "no match\n";
I've since figured it out, but since it was so much fun, I'll leave the answer for you to figure out :)

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re: Regex puzzle for you
by Zaxo (Archbishop) on Aug 03, 2004 at 23:46 UTC

    Update: De-spoilerized.

    After Compline,
    Zaxo

Re: Regex puzzle for you
by FoxtrotUniform (Prior) on Aug 03, 2004 at 23:59 UTC

    Update: spoiler hidden. (Thanks Zaxo for being clever about it.)

Re: Regex puzzle for you
by injunjoel (Priest) on Aug 04, 2004 at 00:59 UTC
    Greetings all,
    As far as I understood it the underscore is part of the \w character class in regex, so your /\b_+(\w+)_+\b/ worked the way it should have. If you just want the x's try /\b_+([a-z]+)_+\b/i next time.

    -injunjoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

      Or [[:lower:]]+ if you want to respect locale.

Re: Regex puzzle for you
by ccn (Vicar) on Aug 04, 2004 at 07:43 UTC

    Replying from the front page I have not seen other replies.
    To having answer one must remember that

    • regexps are greedy
    • \w matches _
    • regexps do backtrack to get a match
    '__xxx__' =~ m/ \b # word boundary, matches begin of the string _+ # matches a couple of '_' because greedy (\w+) # matches the rest(greedy) but must do backtrack for whol +e regexp match _+ # after one backtrack matches last '_' \b /x;
Re: Regex puzzle for you
by johnnywang (Priest) on Aug 04, 2004 at 02:07 UTC
    add a "?":
    print '__xxx__' =~ /\b_+(\w+?)_+\b/ ? "$1\n" : "no match\n";
Re: Regex puzzle for you
by Gilimanjaro (Hermit) on Aug 04, 2004 at 10:37 UTC
    Will work if you make it (\w+?) instead... And I didn't look at any other replies! What did I win?
      Jack, tell him what he's won!

      You've won a 1965 Food Processor by Oster! Unfortunately, the instructions were fed to the machine in 1982 on Christmas Day when it caused the souffle to fall. It has since been run over by a giant Tonka truck, and will be delivered in a yard bag, as soon as we can find the last 3 screws that fell down the gutter drain.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        Yay!
        And There Was Much Rejoicing!
Re: Regex puzzle for you
by Anonymous Monk on Aug 04, 2004 at 03:27 UTC

    I did this:

    print '__xxx__' =~ /\b_+(\w+[^_])_+\b/ ? "$1\n" : "no match\n";

    Honestly didn't seem that hard (assuming your goal was to strip the underscores)

      I understand your point with:
      /\b_+(\w+[^_])_+\b/
      But this is not equivalent, as it requires at least 2 characters in the parens, whereas my original
      /\b_+(\w+)_+\b/
      only requires 1.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

Re: Regex puzzle for you
by ambrus (Abbot) on Aug 04, 2004 at 18:10 UTC

    My guess is this:

    Spoiler:

    It prints xxx_ because \w matches _.

Re: Regex puzzle for you
by Jasper (Chaplain) on Aug 05, 2004 at 16:10 UTC
    The biggest puzzle is how a perl saint got bitten by this at all. ;)
      I think it's more of a puzzle that you think they don't! :p

      A few actually post their D'oh experiences, for the rest of us to learn from [and sometimes laugh at [quietly [ to ourselves ]]].

      I propose that anyone who doesn't experience these episodes isn't human, or isn't truthful.

      Besides, it doesn't take much to get to saint level anyway. Login every day, vote on stuff, and ask useful questions. You never have to contribute anything original, but it helps. [It seems someone posted once how long it takes to become a saint without posting, but I forget the answer.]

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        Besides, it doesn't take much to get to saint level anyway. Login every day, vote on stuff, and ask useful questions. You never have to contribute anything original, but it helps. [It seems someone posted once how long it takes to become a saint without posting, but I forget the answer.]
        We have a a Saint without nodes (he might have posted some nodes that got deleted later, I don't know.)
        It seems someone posted once how long it takes to become a saint without posting, but I forget the answer.
        See Sainthood via Seniority Simulation which comes up with 450 to 500 days of daily login and using all your votes.