• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regex question

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are there any other characters that are going to cause me problems in the replacement string?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help everyone.
 
reply
    Bookmark Topic Watch Topic
  • New Topic