Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - matching world which contain only vowels or consonants

Tags:

regex

I have problem with regex. I want to match only these words which contains only vowel or consonants.

For example I have strings like this:

aeyiuo 
aeYYuo 
qrcbk 
aeeeee 
normal 
Text 
extTT 

My regex should match: aeyiuo aeYYuo aeeeee

I don't know why my expression doesn't work ^[aeyiuo]*|[^aeyiuo]*$ To check my regexp I use online parsers: http://www.regexr.com/ or http://regexpal.com/

If somebody could explain me my mistake, I would be very grateful.

like image 697
Araneo Avatar asked Dec 05 '22 07:12

Araneo


1 Answers

If you want to match lines with only vowels then you just need to think about a character class [].
What should we add to it ? Vowels ! [aeiouy].
We need it one or more times, so let's add a plus sign to it [aeiouy]+.
We also need to anchor it ^[aeiouy]+$.
Now let's talk about the modifiers, we need the i modifier to match case insensitive. We also need the m modifier so that ^ and $ will match begin of line and end of line respectively. Also don't forget the g modifier, in some engines you need it to match all occurences.

Online demo

like image 177
HamZa Avatar answered Feb 23 '23 07:02

HamZa