Friday, 27 September 2013

Java Regex pattern to match string with apostrophes

Java Regex pattern to match string with apostrophes

I need to match a pattern to strip the 's as well as the ending ' from
words. I've used the regex pattern (\w+)('s$|s'$|'$). I need the first
group. However what is happening with this pattern is a word like cats'
matches with the third pattern i.e '$. Hence my group(1) still has cats. I
tried using the pattern (\w+)('s$|s'$|([^s] & '$).But here what happens is
for a word abc' group 1 has just ab and group 2 has c'. Any suggestions on
how to handle this.

No comments:

Post a Comment