tags:

views:

79

answers:

2

I am learning regular expressions and having a hard time. Can anyone tell me if I'm on the right path with these two problems?

-List the words in the language specified by the regular expression (a|b)(c|elipson) -- I am thinking the answer is ac, bc, a,b. Am I right?

-Give a regular expression recognizing all words with an odd number of a’s. -- I am thinking (a)(aa)* . If I'm not wrong this should always work with the exception when the word is just 'a'. how can I modify this to make it work when the word is just 'a'?

+4  A: 
  • Assuming "epsilon" means the empty string, then you are correct.

  • You are also correct with (a)(aa)*. Look up what * means in your regular expression syntax (and compare it to the meaning of +).

Greg Hewgill
+1  A: 

Your second answer only gives words that have 1, 3, 5, ... a's in sequence. If you want all words that contain - at any place - an odd number of a's, you'll want somethig like this:

/a([^\s]*a[^\s]*a)*/

If you're strict and don't want to get words with hypens or other non-letter-chars, this should do:

/a([\w]*a[\w]*a)*/

(Depending on the RegEx Engine, you need to replace [\w] with [a-z]

Pascal