From: Garance A Drosehn Date: 2005-08-01T14:15:42+09:00 Subject: Defining regexp's and variables set by them Sometimes I get in a situation where I have a case statement with a several when-clauses, each of which is a regular expression. Some of those regular-expressions may be rather complicated. If that case statement is going to be processed many times, then I like to define objects via Regexp.new, and use those pre-compiled objects in the when clauses: re_simple = Regexp.new("check (\d+)") ... very_huge_file.each_line { |aline} case aline when re_simple check_number = $1 when re_other ... The downside to this is that the regular-expression is now defined somewhere "far away" from the when-clause that uses it. After some testing I may need to make changes to the regexp, such as: re_simple = Regexp.new("(check|test) (\d+)") The thing is, by changing 'check' to '(check|test)', I have to remember that the when clause also needs to change from referencing $1 to referencing $2. Note that in this case I do not *care* whether it matched 'check' as opposed to 'test'. Either word is acceptable to me, so I do not need the actual value of $1 for anything. I was kinda wondering if it would make sense for ruby to support something like: re_simple = Regexp.new("check (\d+)") { cnum = $1 } so I could then have the when clause say: when re_simple check_number = cnum I realize this is a trivial example, but as the expressions get more involved, and the case-statement has many when's, it would be nice if I could have the compiled regular-expression set values on variable names that *I* pick, in addition to the standard values in a MatchData object. Another thing that this might let me do, is something like: when re_simple, re_other, re_yetmore check_number = cnum where the different regular-expressions may find 'cnum' in different positions in the string ($1 vs $2 vs $3), and yet they could all be processed in the same when-clause. Or is there already some way to do this? -- Garance Alistair Drosehn = drosihn@gmail.com Senior Systems Programmer or gad@FreeBSD.org Rensselaer Polytechnic Institute; Troy, NY; USA