4 ms·
Named captures are a great way to document your regular expressions. Don't use the "#" notation to comment on what the regex is doing, just name the group inste
by viggity 16y ago
Named captures are a great way to document your regular expressions. Don't use the "#" notation to comment on what the regex is doing, just name the group instead!
.Net has had them for a long, long time and they are AWESOME!
It'd be cool to see Ruby get something like a MatchEvaluator, too
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx http://msdn.microsoft.com/en-us/library/system.text.regulare...
A MatchEvaluator lets you much more complicated regex find replace. Currently, find/replace only lets you re-order or remove groups or insert static text. A MatchEvaluator lets you handle what to replace the match with code - meaning you can call a webservice that will replace MM/DD/YY with the equivalent Mayan/Persian/Lunar date.
- igrigorik 16y agoSpeaking of named captures.. Something I didn't mention in the blog post, but a rather handy feature: ruby-1.9.2-p136 :011 > /My name is (?<first>\w+) (?<last>\w+)/ =~ "My name is Ilya Grigorik" ruby-1.9.2-p136 :012 > first "Ilya" ruby-1.9.2-p136 :013 > last "Grigorik"
- rue 16y ago> Currently, find/replace only lets you re-order or remove groups or insert static text. I must be misunderstanding you, but that's obviously incorrect. "foo 1 bar 2".gsub(/\d+/) {|m| m.to_i * 2 } 1.8 also. Does MatchEvaluator bring more to the table?
- viggity 16y agothat looks exactly like a match evaluator, so it looks like ruby does have that ability. Awesome.