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.
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? Or is there a better way to do this?
Ah ok. So how can I get it to repeat without overwriting the existing
values for the group? Or is there a better way to do this?
I would do it somehow like:
========== code ==========
texts = [ “”,
“”,
“”]
texts.each do |txt|
if (md=txt.match(/<(\w+?)((?:\s\w+=\w+)+)>/))
puts “\nkey ‘#{md[1]}’ found”
md[2].scan(/\s(\w+)=(\w+)/) do |k, v|
puts " parameter ‘#{k}’ has value ‘#{v}’"
end
else
puts “+++ no match for ‘#{txt}’”
end
end
========= result =========
key ‘orderMsg’ found
parameter ‘biz’ has value ‘0’
key ‘orderMsg’ found
parameter ‘type’ has value ‘7’
parameter ‘size’ has value ‘0’
key ‘orderMsg’ found
parameter ‘type’ has value ‘7’
parameter ‘size’ has value ‘0’
parameter ‘biz’ has value ‘1’
========== end ===========
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 = [
" ",
" ",
" ",
]
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