From: Paddy3118 Date: 2007-02-03T17:45:24+09:00 Subject: Re: Named groups in regexp matches? On Jan 29, 10:14 pm, Wolfgang N�dasi-Donner wrote: > Tom Pollard schrieb:> Does Ruby support regexps that assign names to specific matched groups? > > In Ruby 1.9 it works. I wrote some artikels with many examples onhttp://www.ruby-mine.de(the site may be down for maintenance the next two days, > especially the following one:http://www.ruby-mine.de?p=130- unfortunately it > is only available in german in the moment, but the examples are Ruby code and > irb usage, so it should be understandable without understanding the german texts. > > But - Ruby 1.9 is still under development. May be that there will be changes in > details in future. > > Some examples: > > irb(main):001:0> md="abba".match(/(?.)(?.)\k\k/) > => # > irb(main):002:0> md[0] > => "abba" > irb(main):003:0> md[1] > => "a" > irb(main):004:0> md[2] > => "b" > irb(main):005:0> md[:a1] > => "a" > irb(main):006:0> md[:a2] > => "b" > irb(main):007:0> md['a1'] > => "a" > irb(main):008:0> md['a2'] > => "b" > > Here it is visible, that the contents of a matched groups are accessible by > number, name as symbol, and name as string, but it is not allowed to mix named > groups and normal capturing groups in the same regular expression: > Hi Wolfgang, I was going to ask why you did not use the syntax of (?P...) as used in Python, but found that, according to http://www.amk.ca/python/ howto/regex/regex.html#SECTION000530000000000000000, the P is for Python extensions. But then if the Ruby extension is quite like the Python one, but not seen as being the canonical implementation of grouped expressions then maybe it should be (?R...) showing that this is a ruby-specific etension? Thanks, - Paddy. > Summary - in the near future you will habe a lot of powerful new features in > Ruby's pattern matching facilities. > > Wolfgang N�dasi-Donner