Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: what means this regex? $x = qr/[0-9a-f]{4|8}/

by Zaxo (Archbishop)
on Jun 09, 2006 at 08:30 UTC ( #554444=note: print w/replies, xml ) Need Help??

in reply to what means this regex? $x = qr/[0-9a-f]{4|8}/

Did you expect to get bitwise-or evaluated to match twelve characters? Or did you expect regex alternation to get it to match exactly four or exactly eight?

I think the regex engine gave up on compiling a quantifier when it hit incompatible syntax. That would leave the braces as literal.

After Compline,

  • Comment on Re: what means this regex? $x = qr/[0-9a-f]{4|8}/

Replies are listed 'Best First'.
Re^2: what means this regex? $x = qr/[0-9a-f]{4|8}/
by kidongrok (Acolyte) on Jun 09, 2006 at 14:32 UTC
    That was a poorly worded Q you responded to. To be clear, it was a muse, rather than write "4 or 8 hex chars" other longer ways. You saw a 2nd interpretation that I had dismissed:
  • Doing bitwize-or: {4|8} = {12}; gives only 1 match-length, which is already doable '{12}'
  • Doing match-length-alternation, insofar as it allows multiple lengths to be given, seems more useful.

      If alternation worked in quantifiers, you'd want to put the eight first. The regex engine may be greedy, but it's also hasty. As soon as it matches an alternate it forgets about the remaining ones. Anything that would match the eight has already matched the four.

      Translating to the intended regex,

      $_ = "abc" x 4; $re_short = qr/([0-9a-f]{4}|[0-9a-f]{8})/; $re_long = qr/([0-9a-f]{8}|[0-9a-f]{4})/; print $1, $/ if /$re_short/; print $1, $/ if /$re_long/; __END__ abca abcabcab

      After Compline,

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2021-10-20 00:05 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (78 votes). Check out past polls.