Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Regexp not capturing in named subrules

by diotalevi (Canon)
on Sep 16, 2009 at 16:35 UTC ( #795660=note: print w/replies, xml ) Need Help??


in reply to Re: Regexp not capturing in named subrules
in thread Regexp not capturing in named subrules

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"

Replies are listed 'Best First'.
Re^3: Regexp not capturing in named subrules
by ikegami (Pope) on Sep 16, 2009 at 16:57 UTC
    (?<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.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        The grammar section (?: ... ){0} creates the subroutines...
        I'm really confused. Did you invent that construct yourself (which looks broken to me) or is it something special-cased in recent grammars (away from its pointless purpose) and recommended in some document I haven't read yet?

        Because if I was writing a regex engine, I'd actually turn {0} into a fatal error, with the invoker needing a clue-bat.

        -- 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://795660]
help
Chatterbox?
[1nickt]: Corion this looks good, from the pod: "there are 9000+ variations that are detected correctly in the test files (see t/data/* for most of them). If you can think of any that I do not cover, please let me know."
[TCLion]: some moron put the date like this : Mon Feb 20 09:31:30 2017
[Corion]: 1nickt: Yes, the module sounds promising indeed
[1nickt]: "putting the date in correct order" how?
[Corion]: TCLion: Whee ;)
[TCLion]: need to put like this : 2017-02-20 09:30:53
[1nickt]: That's why I asked if you are using DateTime. It has a large number of supporting modules (the author likes the term 'eco-system') so if you are already creating a DateTime obj from your dates, this module would read in the mnoron-formatted 1s seamlessly
[TCLion]: when I put the date together it looks like : 2017-Feb-24 (month is the problem)
[1nickt]: good luck, then.
[TCLion]: looking at DateTime documentation in monastery now

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2017-03-23 14:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (288 votes). Check out past polls.