# How to do this regex substitution?

**URL:** <https://www.ruby-forum.com/t/how-to-do-this-regex-substitution/137126>\
**Category:** Ruby\
**Created:** [May 9, 2008, 4:45am UTC](https://www.ruby-forum.com/t/how-to-do-this-regex-substitution/137126 "2008-05-09T04:45:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![globalrev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@globalrev](https://www.ruby-forum.com/u/globalrev)\
**Post date:** [May 9, 2008, 4:45am UTC](https://www.ruby-forum.com/t/how-to-do-this-regex-substitution/137126/1 "2008-05-09T04:45:50Z")

</div>

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.

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

---

<div class="post-metadata">

**Author:** ![globalrev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@globalrev](https://www.ruby-forum.com/u/globalrev)\
**Post date:** [May 9, 2008, 5:30am UTC](https://www.ruby-forum.com/t/how-to-do-this-regex-substitution/137126/2 "2008-05-09T05:30:18Z")

</div>

From: globalrev [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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” =~ /(.)o\1/  
#=\> 0

“testhoh” =~ /(.)o\1/  
#=\> 4

$1  
#=\> “h”

if “testhoh”.match(/(.)o\1/)  
“match!”  
else  
“sorry”  
end  
#=\> “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”]]  
#=\> “qwtyasdfghj”

“qwxoxtyasdfzozghj”.gsub(/(.)o\1/) do |m|  
$1  
end  
#=\> “qwxtyasdfzghj”

“qwxoxtyasdfzozghj”.gsub(/(.)o\1/) do |m|  
“\<#$1\>”  
end  
#=\> “qwtyasdfghj”

“qwxoxtyasdfzozghj”.gsub(/(.)o\1/){“\<#$1\>”}  
#=\> “qwtyasdfghj”

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

kind regards -botp

---

<div class="post-metadata">

**Author:** ![globalrev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@globalrev](https://www.ruby-forum.com/u/globalrev)\
**Post date:** [May 10, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/how-to-do-this-regex-substitution/137126/3 "2008-05-10T19:55:36Z")

</div>

globalrev [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> 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.
> 
> 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”  
`----
