Regex: how to match duplicate syllables?

B

basi

Merry Christmas!

But work goes on :(

Just trying a way to match duplicate syllables.

C = "bcdfghjklmnpqrstvwxyz"
V = "aeiou"

aString =~ /[#{C}][#{V}][#{C}][#{V}]/

should match if the first CV matches the next CV. Hence, "waikiki"
should match on "kiki". And "dodo", "paparazzi" should match too.

Thanks!
basi
 
J

James Edward Gray II

Merry Christmas!

Back at ya. ;)
But work goes on :(

Just trying a way to match duplicate syllables.

C = "bcdfghjklmnpqrstvwxyz"
V = "aeiou"

aString =~ /[#{C}][#{V}][#{C}][#{V}]/

should match if the first CV matches the next CV. Hence, "waikiki"
should match on "kiki". And "dodo", "paparazzi" should match too.

Hope this helps:
V = "aeiou".freeze => "aeiou"
C = ("a".."z").to_a.join.gsub(/[#{V}]/, "").freeze => "bcdfghjklmnpqrstvwxyz"
DBL_SYL = /([#{C}][#{V}])\1/.freeze => /([bcdfghjklmnpqrstvwxyz][aeiou])\1/
%w{waikiki dodo paparazzi}.each do |test| ?> puts "#{test} => #{$&}" if test =~ DBL_SYL
end
waikiki => kiki
dodo => dodo
paparazzi => papa
=> ["waikiki", "dodo", "paparazzi"]

James Edward Gray II
 
M

Mike Stok

Merry Christmas!

But work goes on :(

Just trying a way to match duplicate syllables.

C = "bcdfghjklmnpqrstvwxyz"
V = "aeiou"

aString =~ /[#{C}][#{V}][#{C}][#{V}]/

should match if the first CV matches the next CV. Hence, "waikiki"
should match on "kiki". And "dodo", "paparazzi" should match too.

What about using \1 e.g.

C = "bcdfghjklmnpqrstvwxyz"
V = "aeiou"

%w{ kiki dodo paparazzi abacab }.each do |word|
puts "#{word} matched" if word =~ /([#{C}][#{V}])\1/
end

Hope this helps,

Mike

--

Mike Stok <[email protected]>
http://www.stok.co.uk/~mike/

The "`Stok' disclaimers" apply.
 

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
474,176
Messages
2,570,947
Members
47,499
Latest member
DewittK739

Latest Threads

Top