• Post Reply Bookmark Topic Watch Topic
  • New Topic

regex question  RSS feed

 
Benjamin Hundley
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me why this throws an IllegalArgumentException?

public class VariableSubstitutionTest{
public void setup(){
String dollarSignString = "${Hello}";
System.out.println(dollarSignString);

System.out.println(dollarSignString.replaceAll("\\$\\{Hello\\}", "My Hello Value has a $ in it"));
}

public static void main(String[] args) {
new VariableSubstitutionTest().setup();
}
}



If I take the dollar sign out of the replacement value then it works.
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The dollar sign is a meta character: it means 'end-of-line'. try delimiting it using \\ or maybe \\\\ or maybe \\\\\\

see which one works. And try to figure out why
 
Benjamin Hundley
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The $ is only a meta-character in the context of regular expressions correct? I didn't think that the replacement value was a regular expression.

I tried this with a ^ in place of the $ and it worked fine. I thought ^ was a meta-character too.

(It does work when I put \\ in front of the $ in the replacement value. I'm just not exactly sure why.)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Benjamin Hundley:
The $ is only a meta-character in the context of regular expressions correct? I didn't think that the replacement value was a regular expression.

I tried this with a ^ in place of the $ and it worked fine. I thought ^ was a meta-character too.

(It does work when I put \\ in front of the $ in the replacement value. I'm just not exactly sure why.)



The replacement string is *not* a regular expression. However, it is not just a plain string either. The "$" has a special meaning.

Basically, you can build a replacement string using groups that were captured during the regex match. $0 is group zero, $1 is group one, etc...

Henry
 
Benjamin Hundley
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any other characters that are going to cause me problems in the replacement string?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Benjamin Hundley:
Are there any other characters that are going to cause me problems in the replacement string?


The other one is the "\" (backslash). It is used to escape the "$" and "\".

Henry
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at http://java.sun.com/javase/6/docs/api/java/util/regex/Matcher.html#quoteReplacement(java.lang.String) for more info
 
Benjamin Hundley
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help everyone.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!