Now that we are on the topic of Regular Expressions I have a question
about the ruby implementation. Like I posted earlier I needed to parse
something that looks like this:
- activity
+ name
+ picture
- area
+ name
+ picture
- activities_in_area
+ activity_id
+ area_id
etc...
Last time I did complicated regexps seems to have been C# or possibly
java. So I tried to match the whole thing with
/(\- (\w*)\s*?\n([\t ]+\+ (\w+)\s*(\:\s*(\w*))?\s*?\n)+\s*)+/
and then I was gonna extract the captured data but it isn't available.
All nested groups have only captured their latest match. Is there no
regexp lib for ruby that can handle nested groups and save the
captures? I solved it with nested scans instead and I have to admin
that it is more readable, so I'm not sure what, exactly, I want with
this message, except ask about the design choices involved. Why don't
we want proper captures?
table_name = /\- (\w*)\s*?\n/
field_name = /(\s+\+ (\w+)\s*(\:\s*(\w*))?\n)/
doc.scan /#{table_name}(#{field_name}+)/ do |tablename, fields|
fields.scan field_name do |junk, fieldname, junk2, type|
# here I can do what I want
end
end
And on the topic of pattern matching, can you recommend any good
library for parser generation in ruby? I want to write a grammar and
get an AST.
einarmagnus