Sunday, 18 August 2013

Regular expression match if there's non-alphabetical character at the end, or nothing?

Regular expression match if there's non-alphabetical character at the end,
or nothing?

I have some regular expressions that match homonyms, like tw?oo? would
match either two, to, or too. (It's also matches twoo, but that's ok).
My question is, I want the regular expression to match if there is
punctuation or some other nonalphabetical character at the ends, like
"to," or "two." or even ",too!". If there's nothing at the end, that's ok
as well.
So I want it to match tw?oo? if there's no other characters on each side,
OR there are non-alphabetical characters, but NOT if there are letters
around, like "TOmorrow" shouldn't match.
I tried [^A-Za-z]?tw?oo?[^A-Za-z]? , but since the character classes are
option they just get ommitted.
How would I do this, so the regex only matches the words if they are on
their own, or surrounded by punctutation. (spaces aren't a problem,
they've been cut out)
Thanks!

No comments:

Post a Comment