Regex - Match lines that contain at least 2 vowels

144 Views Asked by At

I want to find all lines that contain at least two vowels.

I tried to use the sentence: .*[aeiou].{2,}\n
But it seems that it doesn't succeed, in this failed result it still contains even lines with only one vowel such as "abc123"enter image description here

3

There are 3 best solutions below

2
sharcashmo On

What about something as simple as .*[aeiou].*[aeiou]?

Your {2,} applies to the dot just before.

0
user15460159 On

I don't know what your definition of a sentence is, but the following matches two vowels each followed by anything (even other vowels).

([aeiou].*){2}

0
The fourth bird On

If you want to match the whole line, you can use a negated character class to prevent backtracking:

^(?:[^aeiou\n]*[aeiou]){2}.*

Explanation

  • ^ Start of string
  • (?: Non capture group to repeat as a whole part
    • [^aeiou\n]*[aeiou] Optionally repeat matching any char except the listed or a newline followed by matching one of a e i o u
  • ){2} Close the non capture group and repeat it 2 times
  • .* Match the rest of the line

Regex demo