duplicated match question

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 !
 
C

Charles Mills

Hi,

I am new to Ruby,and confusing about following regular patterns:

why pattern "(\w)\1" match duplicated letter "ll" in "Hello"

(\w) matches a word character and stores it in the first 'register'.
\1 is the literal value of the first register.

So, /(\w)\1/ matches a word character followed by that same word
character.
irb(main):004:0> /(\w)\1/ =~ 'll'
=> 0
irb(main):005:0> /(\w)\1/ =~ 'aa'
=> 0
irb(main):006:0> /(\w)\1/ =~ 'bb'
=> 0
irb(main):007:0> /(\w)\1/ =~ '11'
=> 0
irb(main):008:0> /(\w)\1/ =~ '##'
=> nil
'#' is not a word character.
and "(\w+)\1" match "ississ" in "Mississippi"?

/(\w+)\1/ matches a sequence of word characters followed by that same
sequence of word characters.
In 'Mississippi' the (\w+) matches 'iss' and the \1 which resolves to
'iss' matches 'iss' a second time, giving a complete match of
/(\w+)\1/.

Also note:
irb(main):009:0> /(\w+)\1/ =~ 'ssissi'
=> 0
irb(main):010:0> $1
=> "ssi"
irb(main):011:0> $&
=> "ssissi"

Hope that helps,
Charlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top