I cannot capture regex from the end of the string with the Java replace method

Asked

Viewed 51 times

0

I tried that code but the $ is not understood as the end of the string, but rather as a character to be captured, I did something wrong or it’s a bug?

String s = "gabriel";
System.out.println(s.replace("iel$", ""));

1 answer

0


According to the documentation, the method replace nay works with regular expressions.

What you want is the method replaceAll. This yes works with regex:

String s = "gabriel";
System.out.println(s.replaceAll("iel$", "")); // gabr

Although in that particular case no need to regex:

String s = "gabriel";
System.out.println(s.replace("iel", ""));
  • MY HERO VLWWW

Browser other questions tagged

You are not signed in. Login or sign up in order to post.