Is it possible for me to create a regex which indicates something along these lines: Pattern.compile("marsu[pial]"); where Pattern matches only "marsup", "marsupi", "marsupia", and "marsupial"?
I had originally thought it was, and that was the proper syntax, but then I realized that the above regex also matches "marsul", or any of the other bracketed letters or combination thereof.
After doing some research the best I can come up with is: Pattern.compile("(marsup)|(marsupi)|marsupia)|(marsupial)"); But that's not really working for me efficency-wise, nor does it seem much better than simply creating String and running String.equals.
Actually, "[pial]" matches exactly one of the letters 'p', 'i', 'a', or 'l' each time it's applied. The order in which they're listed has no effect on the order in which they're matched. Here's a regex that will optionally match on or more of those letters, but only if they're in the correct order:
The first regex works, though I added $ so that the user couldn't correctly enter something like marsuphorsecowpie and still get a match.
The second one appears to only respond to 'marsupial.' However, if changed to: marsu[p]?[pi]?[pia]?[pial]?$ It becomes a lot more functional, but has the issue of matching strings like following: marsuppipia marsupipia marsupiapial
Another option which seems to work identically to the first regex is: marsu(p|(pi)|(pia)|(pial))?$
EDIT: Oh. And thank you both for responding.
[ December 18, 2008: Message edited by: Alex Birmingham ] [ December 18, 2008: Message edited by: Alex Birmingham ]
Thats funny, yea I don't doubt that mine was wrong, regexes are my next area of indepth study... I didn't even know you could use () in them... (G) or conditional statements for that matter. However, I must say that knowing about conditionals is going to make the matter less daunting.
Hate to keep this thread alive behind it's natural life, but what about... a regex represents an article, an adjective, and a noun, and matches either just the (partial or whole) noun, or (partial or whole) adjective + (partial or whole) noun, or article + adjective + noun (all partial or whole). Here's what I've got after reading the abovementioned tutorial: ^(a.?)?(g(r(e(y.?)?)?)?)?d(o(o(r)?)?)?$
It's 99 percent there, however it doesn't match: 'a gr door' 'a gre door' or any other string in which grey is partial.
Any hints as to why not? Other parts of speech match partially.