Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove "${anything}" from string in java

Tags:

java

regex

I want to remove ${anything} or ${somethingelse} from a string, but i dont find the regex.

My actual code

String url = http://test.com/index.jsp?profil=all&value=${value}
String regex = "\\$\\{*\\}";
url = url .replaceAll(regex, ""); // expect http://test.com/index.jsp?profil=all&value= 
//but it is http://test.com/index.jsp?profil=all&value=${value}

i'm sure the solution is stupid, but no way to find.

like image 426
Antoine Avatar asked Feb 27 '23 03:02

Antoine


2 Answers

Try this one:

"\\$\\{.*?\\}"

The .*? matches the shortest possible string that is followed by }.

like image 157
tangens Avatar answered Mar 08 '23 05:03

tangens


you're removing any number of {'s, because you have {* instead of .*

should be \\$\\{.*\\}

that will indeed allow anything between the braces, do you want that to be alpha only or something?

that would be \\$\\{[a-zA-Z]*\\}

like image 21
John Gardner Avatar answered Mar 08 '23 06:03

John Gardner