I have no clue how to solve this one?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
alexej83
Forum Newbie
Posts: 2
Joined: Tue Apr 07, 2009 8:11 pm

I have no clue how to solve this one?

Post by alexej83 »

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.
All help is welcome. :banghead:
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: I have no clue how to solve this one?

Post by prometheuzz »

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
alexej83
Forum Newbie
Posts: 2
Joined: Tue Apr 07, 2009 8:11 pm

Re: I have no clue how to solve this one?

Post by alexej83 »

Thank you.
When you see the solution you always feel so stupid and it looks so easy. :D
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: I have no clue how to solve this one?

Post by prometheuzz »

alexej83 wrote:Thank you.
When you see the solution you always feel so stupid and it looks so easy. :D
You're welcome.
Post Reply