My professor gave us this pattern "/[aeiou]+[1234567890]*[a-z]?/" and I'm trying to find matches in "ae12abc r2d2 a1steaksauce".

The resulting matches the professor gave us are ae12a, a1s, eak, auc, and e.

I think I'm totally misunderstanding how this is supposed to work. Why are ae12abc, r2d, or a1steaksauce not possible matches? Thanks in advance, I really appreciate any help with this!

  • 2
    $\begingroup$ An important question is, do you want all matches or just maximal matches? $\endgroup$
    – Raphael
    Feb 18 '15 at 8:34

Well let's walk through it. You need one or more vowels, followed by any number of numbers (including none), followed by (optionally) a letter.

ae matches your vowels, 12 matches the numbers, then a counts as your optional letter. The engine starts again and looks for the next vowel. It skips bc r2d2 since there are no vowels there.

Then it finds a for your vowels, 1 for your numbers, and s as the optional letter. Then the engine starts again and looks for the next vowel.

It finds ea for your vowels. There are no numbers, so it grabs k as the optional letter. Then it starts again looking for vowels...


Why ae12abc is not a match: ae matches the [aeiou]+ part of the regexp, 12 matches the [1234567890]* part, but abc doesn't match [a-z]?, because [a-z]? matches the empty string or any single letter, and abc is not a single letter.

Why a1steaksauce is not a match: the same reason. steaksauce is not a single letter.

Why r2d is not a match: r doesn't match [aeiou]+.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.