0

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+}.

  • possible duplicate of [How to escape text for regular expression in Java](http://stackoverflow.com/questions/60160/how-to-escape-text-for-regular-expression-in-java) – DavidPostill Jun 29 '14 at 07:42
  • @DavidPostill I don’t see how that question is a duplicate of this one. – tchrist Jun 29 '14 at 12:52
  • It’s really much better if these sorts of questions include samples of actual valid (and perhaps invalid) inputs. Is there any chance the text you’re matching may contain non-ASCII word-characters? For example, if your string were `"We have seen ${El Niño} hurricanes."` – tchrist Jun 29 '14 at 12:56

2 Answers2

1

You can try using this regex:

"\\$\\{\\w+\\}"

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

String s = "abc ${test}def"; // for example
s = s.replaceAll("\\$\\{\\w+\\}", "STACKOVERFLOW");
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • I don’t think `replaceAll` is going to work for this, although we cannot be sure because the problem spec is too meagre. I’m betting that the OP wants to use the `\w+` part for something else, too, and so a contant replacement string won’t work. – tchrist Jun 29 '14 at 13:09
1

[^}]* 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 regexMatcher.group()
} 

Explanation

  • \$ matches the literal $
  • \{ matches an opening brace
  • [^}]* matches any chars that are not a closing brace
  • \} a closing brace
zx81
  • 41,100
  • 9
  • 89
  • 105
  • I think your approach is probably going to wind up being the best one, although I’d like to know more about what the OP is trying to do, like look up would-be variables’ values is some HashMap or some such. Call me old-fashioned, but I’d be a lot more comfortable seeing the final close brace escaped as well, since it every bit as literal as the open brace. Also, those are backslashes not slashes. – tchrist Jun 29 '14 at 12:54
  • Thank you for pointing out the `backslash` typo. These unescaped `}` make you twitch... They're acceptable to Java, you only have to unescape the opening one so that it doesn't open a `{quantifier}`. But yes, you're right, escaping them is more eye-pleasing. Updated. :) – zx81 Jun 29 '14 at 13:05
  • The brace twitchiness is because of having been burnt by this oddity once or thrice before, so I just dot my i’s and cross my t’s because I know it’s good for me in the long run, like eating bran flakes for breakfast. I suspect that the original problem to be solved here is not sufficiently specified by the OP, and that the user will want that `\w+` in a capture group so it can be used for further calculations in determining the replacement value, but the OP didn’t really say much about that. If we had real inputs and outputs desired, then we would know for sure — but as is, we don’t. – tchrist Jun 29 '14 at 13:12
  • @tchrist Yeah you're probably right. And the crossing the ts thing is Absolut Klar, don't know why I didn't start there :) – zx81 Jun 29 '14 at 13:15