S
shaobo.li
Hi,
I am new to Ruby,and confusing about following regular patterns:
why pattern "(\w)\1" match duplicated letter "ll" in "Hello"
and "(\w+)\1" match "ississ" in "Mississippi"?
# match duplicated letter
showRE('He said "Hello"', /(\w)\1/) » He said "He<<ll>>o"
# match duplicated substrings
showRE('Mississippi', /(\w+)\1/) » M<<ississ>>ippi
def showRE(str,pt)
md=str.match(pt)
if str=~pt
puts "#{$`}<<#{$&}>>#{$'}"
else
puts "no match"
end
end
any explanation will be great thanks !
I am new to Ruby,and confusing about following regular patterns:
why pattern "(\w)\1" match duplicated letter "ll" in "Hello"
and "(\w+)\1" match "ississ" in "Mississippi"?
# match duplicated letter
showRE('He said "Hello"', /(\w)\1/) » He said "He<<ll>>o"
# match duplicated substrings
showRE('Mississippi', /(\w+)\1/) » M<<ississ>>ippi
def showRE(str,pt)
md=str.match(pt)
if str=~pt
puts "#{$`}<<#{$&}>>#{$'}"
else
puts "no match"
end
end
any explanation will be great thanks !