Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Regexp not capturing in named subrules

by merlyn (Sage)
on Sep 16, 2009 at 15:07 UTC ( [id://795642]=note: print w/replies, xml ) Need Help??


in reply to Regexp not capturing in named subrules

( ... ){0}
Huh?

First, {0} should be outright illegal, because it tends to be used (without effect) by newbies who say "I want this section to contain no K's, so I'll put K{0}".

Second, even if it is legal, as it appears to be, I'd expect any decent optimizer to just completely eliminate it.

So I'm surprised you expected any sort of utility out of your regex.

-- Randal L. Schwartz, Perl hacker

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Replies are listed 'Best First'.
Re^2: Regexp not capturing in named subrules
by diotalevi (Canon) on Sep 16, 2009 at 16:35 UTC

    I guess I was just surprised to find this something that's more possible in Ruby-1.9. I'll still probably end up just translating to a proper parser but it was easy to use the regexp engine to start with. The below snippet is equivalent to my perl but does return the capture.

    require 'pp' re = %r{ # Grammar rules go here (?: (?<thing>.+) ){0} # Invoke grammar here \g<thing> } m = re.match( 'text' ) puts m['thing'] # puts "text\n"
      (?<thing>.+) never matched "text" (backtracking occurred), so it's clearly a major bug in ruby if your code showed it did.

        For clarity, the regex expands to the equivalent of:

        re = %r{ (?: (?<thing>.­+) ){0} (?<thing>.­+) }

        So, yes, (?<thing>.+) did match. It is just that the first instance didn't match. In Perl, %+ only records the captures of the first instance.

        - tye        

        No, you misread. \g<thing> in Ruby-1.9's oniguruma works like (?&thing) in perl 5.10. It's a subroutine call. The grammar section (?: ... ){0} creates the subroutines and I just call an entry point later.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-19 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found