Tom Pollard
2/3/2007 10:00:00 PM
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")
> => #<MatchData:0xb7dfbb5c>
> irb(main):004:0> s=S.new(*m.captures)
> => #<struct S key="name", value="Tom Pollard">
> 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<dir>[\dO]{3}|[0O]|///|MMM|VRB)
(?P<speed>P?[\dO]{2,3}|[0O]+|[/M]{2,3})
(G(?P<gust>P?(\d{1,3}|[/M]{1,3})))?
(?P<units>KTS?|LT|K|T|KMH|MPS)?
(\s+(?P<varfrom>\d\d\d)V
(?P<varto>\d\d\d))?\s+""",
re.VERBOSE)
VISIBILITY_RE = re.compile(r"""^(?P<vis>(?P<dist>M?(\d\s+)?\d/\d\d?|
M?\d+)
( \s*(?P<units>SM|KM|M|U) |
(?P<dir>[NSEW][EW]?) )? |
CAVOK )\s+""",
re.VERBOSE)
RUNWAY_RE = re.compile(r"""^(RVRNO |
R(?P<name>\d\d(RR?|LL?|C)?)/
(?P<low>(M|P)?\d\d\d\d)
(V(?P<high>(M|P)?\d\d\d\d))?
(?P<unit>FT)?[/NDU]*)\s+""",
re.VERBOSE)
TEMP_RE = re.compile(r"""^(?P<temp>(M|-)?\d+|//|XX|MM)/
(?P<dewpt>(M|-)?\d+|//|XX|MM)?\s+""",
re.VERBOSE)
To port these to pre-1.9 Ruby, I'll need to remove the '?P<name>'
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