how to do this regex substitution?

G

globalrev

i want to replaxe the pattrn consonant o consonant, ie hoh -> h, dad -

so i have to check that the letter after the letter i am at is o and
the next one is equal to the one i am at.

so
/qwrtpsdfghjklzxcvbnm/o/qwrtpsdfghjklzxcvbnm isnt true(and doesnt work
anyway).
 
P

Peña, Botp

From: globalrev [mailto:[email protected]]=20
# i want to replaxe the pattrn consonant o consonant, ie hoh -> h, dad -
# > d etc.
# so i have to check that the letter after the letter i am at is o and
# the next one is equal to the one i am at.

hi globalrev,

there are many ways.

best if you can play in irb,

"hoh" =3D~ /(.)o\1/
#=3D> 0

"testhoh" =3D~ /(.)o\1/
#=3D> 4

$1
#=3D> "h"

if "testhoh".match(/(.)o\1/)
"match!"
else
"sorry"
end
#=3D> "match!"

"qwxoxtyasdfzozghj".gsub(/(.)o\1/) do |m|
p [m,$1,$~.pre_match,$~.post_match,$~.captures]
end
["xox", "x", "qw", "tyasdfzozghj", ["x"]]
["zoz", "z", "qwxoxtyasdf", "ghj", ["z"]]
#=3D> "qwtyasdfghj"

"qwxoxtyasdfzozghj".gsub(/(.)o\1/) do |m|
$1
end
#=3D> "qwxtyasdfzghj"

"qwxoxtyasdfzozghj".gsub(/(.)o\1/) do |m|
"<#$1>"
end
#=3D> "qw<x>tyasdf<z>ghj"

"qwxoxtyasdfzozghj".gsub(/(.)o\1/){"<#$1>"}
#=3D> "qw<x>tyasdf<z>ghj"

your next task now is to determine whether a certain char is a consonant =
regardless of case.

kind regards -botp
 
P

Peter Jones

globalrev said:
i want to replaxe the pattrn consonant o consonant, ie hoh -> h, dad -

so i have to check that the letter after the letter i am at is o and
the next one is equal to the one i am at.

so
/qwrtpsdfghjklzxcvbnm/o/qwrtpsdfghjklzxcvbnm isnt true(and doesnt work
anyway).

You question is a bit confusing. You said you wanted to replace:

consonant o consonant

with:

consonant

Yet, one of your examples is dad, which doesn't fit that pattern. Did
you mean "consonant vowel consonant"?

Either way, here is one possibility. I'll leave "dad" as an exercise.

,----
| >> "hoh".sub(/([qwrtpsdfghjklzxcvbnm])o\1/, '\1')
| => "h"
`----
 

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,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top