I want a regular expression pattern that will match with the end of a string.
I'm implementing a stemming algorithm that will remove suffixes of a word.
e.g. for a word 'Developers' it should match 's'.
i can do it using following code :
Pattern p = Pattern.compile("s");
Matcher m = p.matcher("Developers");
m.relaceAll(" "); // it will replace all 's' with ' '
i want a regular expression that will match only a string's end.
basically i want a method replaceLast().
解决方案
You need to match "s", but only if it is the last character in a word. This is achieved with the boundary assertion $:
input.replaceAll("s$", " ");
If you enhance the regular expression, you can replace multiple suffixes with one call to replaceAll:
input.replaceAll("(ed|s)$", " ");