Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

regex

I want to remove "ing" from word, example i want "watching" become "watch" by removing "ing" from the end of the word. Using regex in java.

i use this pattern:

String pattern = "$ing";

and use it to remove:

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

but the result is still "watching" not "watch"

like image 423
Muhammad Haryadi Futra Avatar asked Mar 18 '23 13:03

Muhammad Haryadi Futra


1 Answers

Put the dollar sign at the last, so that it matches the last ing. $ is an anchor which represents End of the line.

Example:

System.out.println("watching".replaceAll("ing$", ""));
like image 99
Avinash Raj Avatar answered Apr 06 '23 11:04

Avinash Raj