views:

431

answers:

1

I've got a long regex in AS3 / Flex which finds one of a couple dozen words. The Regex looks like: word|wordup|wordly|wordster

when I do "wordup wordster!".match(regex) I'm getting undefined maches! the match array that's returned has matches: [0] 'wordup' [1] undefined array length: 2

Is there a known bug in AS3's grouping matches? What could make some words show up in the returned array of matches, and others return as undefind?

I've looked for stray incorrect characters in my regular expression, and inspected the regular expression several times over.

If I just search for 'wordup' then I get a match array length 1, with the correct contents. If I search for just 'wordster' then I get an array length one with matches[0] being undefined again.

------ update -------

After lots of experimenting... my regular expression was just too long for AS3 My actual regular expression used grouping, and had optional parenthesis:

(?:(?(\bword\b))?|(?(\bwordup\b))?| ... and so on for 51 words.

simplifying to: (?:\bword\b|\bwordup\b|

somehow made the match groups work just fine, even though I don't have any parenthesis that would normally be necessary to define groups...

+2  A: 

When you're dealing with "mystery" problems, you should always show your actual code, not something you think is equivalent. word|wordup|wordly|wordster won't give you any "undefined" matches.

Instead of using (?:\bword\b|\bword2\b), use this: \b(?:word|word2)\b

The regex word|(word2)?|word3 will give you zero-length matches, because the second alternative in the regex is optional. It will match the zero-length string at every position in the string where "word" cannot be matched.

Jan Goyvaerts