alexej83 wrote:Hello
I have been trying to solve a school regular expression for hours now. The problem is that i should find all words beginning with a Vowel and ending with a Vowel and then the restriction is that only one consonant is allowed in the middle otherwise anything goes. This is what I came up with
"^[aeiouAEIOU'\.]\{1,\}[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]\{1,1\}[aeiouAEIOU'\.]\{1,\}$"
the problem with it is that it can catch the word U.S.A or i've. Been sitting for hours trying to figure this out.
I presume you meant to write "can't catch", right? So I guess you also want to catch the words "U.S.A." and "I've". If so, the this will do the trick:
Code: Select all
"/(?i)^[aeiou]['.]?[a-df-hj-np-tv-z]['.]?[aeiou]$/"
Note that you need not escape the '.' (DOT) inside a character class.
A (short) explanation:
Code: Select all
(?i) // enable case insensitive matching
^ // start of the string
[aeiou] // match a single vowel
['.]? // match an optional single quote or dot
[a-df-hj-np-tv-z] // match a single consonant
['.]? // match an optional single quote or dot
[aeiou] // match a single vowel
$ // end of the string