Robert Klemme
5/18/2007 7:55:00 AM
On 16.05.2007 22:59, Jon Fi wrote:
> Wolfgang Nádasi-donner wrote:
>> Jon wrote:
>>> B=" <orderMsg type=7 size=0>"
>>> ...
>>> /<(\w+?)(?:\s(\w+)=(\w+))+>/
>>> ...
>>> md[0]=<orderMsg type=7 size=0>
>>> md[1]=orderMsg
>>> md[2]=size
>>> md[3]=0
>> It is correct. "(?:\s(\w+)=(\w+))+" matches two times, the last match is
>> with "size" and "0". The groups will be overwritten each time the "+"
>> will repeat the group.
>>
>> Wolfgang Nádasi-Donner
>
> Ah ok. So how can I get it to repeat without overwriting the existing
> values for the group?
You can't.
> Or is there a better way to do this?
Probably. I am not sure what you are up to but you can use a two stage
approach like this:
texts = [
" <orderMsg biz=0>",
" <orderMsg type=7 size=0>",
" <orderMsg type=7 size=0 biz=1>",
]
texts.each do |t|
p t
md = /<([^\s>]+)((?:\s+\w+=\d+)*)/.match t
if md
tag = md[1]
attrs = md[2]
puts tag
attrs.scan(/(\w+)=(\d+)/) do |m|
print m[0], "=>", m[1], "\n"
end
end
end
Kind regards
robert