johnsondavies
johnsondavies

Reputation: 447

Regular Expression matching a string of specified length

I want to write a regular expression that matches all 9-letter words in a word list that contain only one vowel; for example, "strengths".

The best I've achieved is:

^(([^aeiou]{8}[aeiou])|
([^aeiou]{7}[aeiou][^aeiou]{1})|
([^aeiou]{6}[aeiou][^aeiou]{2})|
([^aeiou]{5}[aeiou][^aeiou]{3})|
([^aeiou]{4}[aeiou][^aeiou]{4})|
([^aeiou]{3}[aeiou][^aeiou]{5})|
([^aeiou]{2}[aeiou][^aeiou]{6})|
([^aeiou]{1}[aeiou][^aeiou]{7})|
([aeiou][^aeiou]{8}))$

(linebreaks added for readability). This works, but is there a way to do it with a more general pattern such as:

^[^aeiou]*[aeiou][^aeiou]*$

by adding a "fail if length is not 9" condition in some way?

Upvotes: 2

Views: 144

Answers (3)

axiac
axiac

Reputation: 72376

adding a "fail if length is not 9" condition in some way?"

The best way is to not check the string length using a regex.

Use the functionality provided by the language you use to check the string length then use a regex to check that it contains only the accepted characters and it includes one wovel.

A simple example in JavaScript (rewriting it in any other language is an easy task):

var input = 'some input string';

if (input.length == 9 && input.match(/[aeiou]/)) {
    console.log('Valid input (length is 9 and contains at least one wovel)');
} else {
    console.log('Invalid input');
}

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627468

You may use

^(?=.{9}$)[b-df-hj-np-tv-z]*[aeiou][b-df-hj-np-tv-z]*$

See the regex demo. Add a case insensitive flag to also match uppercase letters.

*Details

  • ^ - start of a string
  • (?=.{9}$) - a *positive lookahead that makes sure the string contains any 9 chars (else, fail the match)
  • [b-df-hj-np-tv-z]* - 0+ consonants
  • [aeiou] - a vowel
  • [b-df-hj-np-tv-z]* - 0+ consonants
  • $ - end of string.

Upvotes: 1

Toto
Toto

Reputation: 91518

Use a lookahead to limit the length and accept only letters:

^(?=[a-z]{9}$)[^aeiou]*[aeiou][^aeiou]*$

Upvotes: 4

Related Questions