detect $character java regular expression

i have to find a word like ${test} from text file. and will replace the based on some criteria. in the regular express '$' have meaning of search till the end of the line.

what is the regular expression to detect like ${\w+}.


You can try using this regex:


and the method String#replaceAll(String regex, String replacement):

String s = "abc ${test}def"; // for example
s = s.replaceAll("\\$\\{\\w+\\}", "STACKOVERFLOW");

[^}]* rather than \w+ ?

You might want to consider using [^}]* rather than \w+. The former matches any chars that are not a closing brace, so it would allow test-123, which the second would reject. Of course that may just be what you want.

Let's assume this is the raw regex (see what matches in the demo):

  • In Java, we need to further escape the backslashes, yielding \\$\\{[^}]*.
  • Likewise \$\{\w+\} would have to be used as \\$\\{\\w+\}

Replacing the Matches in Java

String resultString = subjectString.replaceAll("\\$\\{[^}]*\}", "Your Replacement");

Iterating through the matches in Java

Pattern regex = Pattern.compile("\\$\\{[^}]*\}");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
       // the current match is


  • \$ matches the literal $
  • \{ matches an opening brace
  • [^}]* matches any chars that are not a closing brace
  • \} a closing brace

Need Your Help

Layout Rendering issue android studio 0.8.0

android xml eclipse android-studio render

I had a working project in eclipse. I made a new project in android studio and remade the same project here by copy pasting the code(I have too much problems with importing, so I preffered to make ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.