How to remove ing from the end of a word using regex in java

I want to remove “ing” from the word, for example, I want the “view” to “look” by removing “ing” from the end of the word. Using regex in java.

i use this template:

String pattern = "$ing";

and use it to delete:

String word = "watching";
word = word.replaceAll(pattern,"");

but the result is still “watching” not “watching”

+4
source share
2 answers

Put the dollar sign last to match the last ing. $is the anchor that represents the end of the line.

Example:

System.out.println("watching".replaceAll("ing$", ""));
+7
source

Or, if you want the true end of the line to use \ z instead

ing\z
0
source

All Articles