Tom P. 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 on
http://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/)
=> #MatchData:0x2bf0488
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:
irb(main):001:0> “abba”.match(/(?.)(.)\2\k/)
SyntaxError: compile error
(irb):1: numbered backref/call is not allowed. (use name):
/(?.)(.)\2\k/
from (irb):1:in `Kernel#binding’
When using “sub”, “gsub”, “sub!”, or “gsub!” witout a block, it is only
possible
to access the groups by name, the positional access return the empty
string
irb(main):001:0> puts ‘axbx’.sub(/(?.)x(?.)x/, ‘\k\k’)
ba
=> nil
irb(main):002:0> puts ‘axbx’.sub(/(?.)x(?.)x/, ‘\2\1’)
=> nil
Inside block a direct access to the group names is not possible - I must
say, I
don’t find a way to do it directly. The use of positional variables “$1”
etc. is
possible. There is another possibility by using the MatchDate object
“$~” inside
the block. In doing this, the same possibilities are available as
described for
“match”:
irb(main):001:0> ‘axbx’.sub(/(?.)x(?.)x/){|k|p k;p $1;p $2;‘u’+$2}
“axbx”
“a”
“b”
=> “ub”
irb(main):002:0> ‘axbxcxdx’.gsub(/(?.)x(?.)x/){|k|p k;p $1;p
$2;‘u’+$2}
“axbx”
“a”
“b”
“cxdx”
“c”
“d”
=> “ubud”
and using MatchData object:
irb(main):001:0> ‘axbx’.sub(/(?.)x(?.)x/){|k|p k;p $1;p $2;‘u’+$2}
“axbx”
“a”
“b”
=> “ub”
irb(main):002:0> ‘axbxcxdx’.gsub(/(?.)x(?.)x/){|k|p k;p $1;p
$2;‘u’+$2}
“axbx”
“a”
“b”
“cxdx”
“c”
“d”
=> “ubud”
There are special situations, where the possibilities of Oniguruma in
Ruby 1.9
allow solutions, which are not as simple to describe in Ruby 1.9.
Ruby 1.8:
irb(main):001:0> “rasbuavb”.scan(/(.)a|(.)b/){|i|p i}
[“r”, nil]
[nil, “s”]
[“u”, nil]
[nil, “v”]
=> “rasbuavb”
Ruby 1.9:
irb(main):002:0> “rasbuavb”.scan(/(.){0}\g<1>a|\g<1>b/){|i|p i}
[“r”]
[“s”]
[“u”]
[“v”]
=> “rasbuavb”
Here isn’t a named group the player, it is the possibility to call a
subexpression. It is a very powerfull feature, which allows recursive
constructs. I made in the article a pocket calculator as example, but it
may
useful for checking complex input fields in a GUI, or even later on in
Rails:
pattern = / (?\g+\g|\g-\g|\g){0}
(?|\g*\g|\g/\g|\g){0}
(?[-+]?\g|(\g)){0}
(?\g|\g){0}
(?[a-zA-Z_]\w*){0}
(?\d+(.\d+)?){0}
^((?\g)=)?(?\g)$
/x
vars = Hash.new(0)
basbind = binding
print ‘input> ‘ # for interactive usage
while (!(inp = DATA.gets).chomp.match(/^quit$/i))
if (md = inp.chomp.gsub(/\s+/,‘‘).match(pattern))
expr = md[:expr].gsub(/([a-zA-Z_]\w*)/, ‘vars[“\1”]‘)
erg = eval(expr, basbind)
vars[md[:var]] = erg if md[:var]
puts “#{inp.chomp}, result> #{(md[:var])?(md[:var]+‘=‘):‘‘}#{erg}”
else
puts “+++++ incorrect input: ‘#{inp.chomp}‘”
end
print ‘input> ‘ # for interactive usage
end
puts ‘***** variables ‘
vars.keys.sort.each{|v|puts “#{v}=#{vars[v]}”}
puts ‘** End ********‘
END
30+12
a = 30 + 12
b = 2a
c = -(aa+5)
d = (6+5*a)*c
quit
results in:
30+12, result> 42
a = 30 + 12, result> a=42
b = 2a, result> b=84
c = -(aa+5), result> c=-1769
d = (6+5*a)*c, result> d=-382104
***** variables *****
a=42
b=84
c=-1769
d=-382104
******* End ********
Summary - in the near future you will habe a lot of powerful new
features in
Ruby’s pattern matching facilities.
Wolfgang Nádasi-Donner