From: Tom Pollard Date: 2007-02-04T06:59:56+09:00 Subject: Re: Named groups in regexp matches? On Feb 3, 2007, at 11:23 AM, Robert Dober wrote: > What I do might be enough for your purpose > > S=Struct.new(:key, :value) > => S > irb(main):002:0> r=%r{(\w+)\s*=\s*(.*)} > => /(\w+)\s*=\s*(.*)/ > irb(main):003:0> m= r.match("name = Tom Pollard") > => # > irb(main):004:0> s=S.new(*m.captures) > => # > irb(main):005:0> s.key > => "name" > irb(main):006:0> s.value > => "Tom Pollard" > > this could easily be wrapped into a class BTW. Thanks. That's not a bad idea, but it only addresses half of my problem, because I still need to be careful to use non-capturing groups for the things I don't want to capture. In Python, I can ignore that - labeling the groups I /do/ want to capture is enough. Here are a few examples from my Python code: WIND_RE = re.compile(r"""^(?P[\dO]{3}|[0O]|///|MMM|VRB) (?PP?[\dO]{2,3}|[0O]+|[/M]{2,3}) (G(?PP?(\d{1,3}|[/M]{1,3})))? (?PKTS?|LT|K|T|KMH|MPS)? (\s+(?P\d\d\d)V (?P\d\d\d))?\s+""", re.VERBOSE) VISIBILITY_RE = re.compile(r"""^(?P(?PM?(\d\s+)?\d/\d\d?| M?\d+) ( \s*(?PSM|KM|M|U) | (?P[NSEW][EW]?) )? | CAVOK )\s+""", re.VERBOSE) RUNWAY_RE = re.compile(r"""^(RVRNO | R(?P\d\d(RR?|LL?|C)?)/ (?P(M|P)?\d\d\d\d) (V(?P(M|P)?\d\d\d\d))? (?PFT)?[/NDU]*)\s+""", re.VERBOSE) TEMP_RE = re.compile(r"""^(?P(M|-)?\d+|//|XX|MM)/ (?P(M|-)?\d+|//|XX|MM)?\s+""", re.VERBOSE) To port these to pre-1.9 Ruby, I'll need to remove the '?P' labels and change the other groups from '(...)' to '(?:...)'. Once I've done that I can worry about assigning labels to the captured groups, after they're matched, or just using the index captures. There's nothing about that that's not straightforward; I'm mostly struggling with my motivation for going through this effort at all, simply to port a working, well-debugged and fairly fast Python module to Ruby, especially since I'm fairly sure the resulting Ruby module will be much slower and harder to maintain. Tom