Chris Ullyott
Chris Ullyott

Reputation: 27

Regex for adjacent vowels and consonants?

I want to write a PCRE-style regular expression that would match a sequence of three or more letters in which a vowel and consonant are adjacent.

So far I am needing to use two patterns, to account for the actual position of the two adjacent characters.

/([aeiou][b-df-hj-np-tv-z]|[b-df-hj-np-tv-z][aeiou])[a-z]/i
/[a-z]([aeiou][b-df-hj-np-tv-z]|[b-df-hj-np-tv-z][aeiou])/i

Any chance I am missing something, and that this can actually be done in just one expression?

Upvotes: 1

Views: 278

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

You can use the following with PCRE:

/[a-z](?:([aeiou])([b-df-hj-np-tv-z])|(?2)(?1))/i

Here, the consonant and vowel matching patterns are captured in their own groups and (?1) and (?2) are regex subroutines that recurse the corresponding group patterns without the need to repeat them.

Upvotes: 1

Rick James
Rick James

Reputation: 142366

/(your first regex)|(your second regex)/i

If you are capturing the result, use [2] instead of [1] -- because of the extra parens.

Upvotes: 1

Related Questions