Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Regex: for any word, find word+s and y/ies

807605Sep 12 2007 — edited Sep 12 2007
Hi all

This sounds pretty simple, but I'm trying to create a regular expression whereby, for any word, the matcher will find all instances of that word OR it's plural.

Now, if it were just adding +s, I know I could do
Pattern.compile("\b\Q" + word + "\Es?\b");
   // note, this forum seems to spaz when I use double-slashes, so I omitted them
However, if it ends in a "y", like "try", I need to find words that end in "ies", like "tries".

At first I thought of making two cases in the pattern, one as above, and one were the last letter is subtracted and you check for (y|ies). HOWEVER, without first knowing if the word ends in a "y", it could take "do" and change it into "dies", and then mistakenly find it.

Of course, I COULD have a whole bunch possible patterns seperated by a bunch of IF-THENs... "if it ends in a y, Pattern pattern = Patern.compile(...."

... but this doesn't seem very elegant.

So, how can I create a pattern that will check for "ies" ONLY if the word ends in "y" (and +"es" if it ends in an "s" and so on...)?

Thanks!
Tim
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 10 2007
Added on Sep 12 2007
2 comments
390 views