Let me painfully honest: I hate parsing, especially w/ regexp, and I don't
care if it's because I stupid and suck at it.
Given your other posts in this forum I cannot believe that you are stupid.
So here are some meta-hints on how to "suck less" at Regexes...
Always use the %r{}x form of regexs.
This neatly avoids the leaning toothpick syndrome when\/matching\/paths
The x modifier allows you to use white space and even comments within the
regex to make it readable. (Larry Wall of perl fame regrets he didn't make
it the default...)
My .emacs has a key-binding that will produce "=~ %r{ }x" and leave the
cursor in the middle.
(global-set-key [(control %)]
`(lambda ()
(interactive)
(insert "=~ %r{ }x")
(backward-char 4)
))
Pull the development of the regex outside the development of your app.
Unit tests are good for that, or even if you just make a wee small script
or do it on the command line or in irb.
If you are doing it on the command line beware of nasty interactions
between the string and quoting conventions of the shell and ruby.
(Speaking Unix now...)
eg. ruby -e "blah" is A Very Bad Idea. The shell will peek inside the
"blah" and do things that you really definitely don't want
happening in a regex. Solution, use single quotes, bash never looks in
side them. Downside, it means you must _never_ use single quotes in the
ruby fragment blah, but you can use double quotes.
ruby -e 'blah'
Grow the regex slowly. Start with the smallest thing, make it match.
If you immediately write down a large regex, odds on it will match
nothing.
Sheer murderous frustration lies that way.
Start small, or strip away stuff on the right hand side of the regex until
you match anything something. Then slowly start adding it back.
File.read(fileName) is cute. It allows you to pull the whole file in at
once as one string and then you can match across lines.
Be aware that since standards are such good things, everyone has their are
own one. ie. POSIX (grep) regexes are different to Emacs regexes which are
different to Ruby regexes. grep even provides too different regex
languages! Ruby and perl regexes are very similar.
It shouldn't have to be this hair pulling!
It isn't. Really. Do what I suggest and you will slowly find regexes are
really a very fun and powerful way of doing things.
John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand
"The notes I handle no better than many pianists. But the pauses
between the notes -
ah, that is where the art resides!' - Artur Schnabel