Home arrow static arrow Java Programming [Archive] - I need some help on regular expression
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - I need some help on regular expression
This topic has 2 replies on 1 page.

Posts:28
Registered: 6/13/01
I need some help on regular expression  
Aug 6, 2004 12:10 AM



 
I have a String such as "abc(def)". I need to replace "(" with "\(" and replace ")" with "\". So I wrote something like this.
    String a = "abc(def)";    a = a.replaceAll("\\(", "\\\\(");    a = a.replaceAll("\\)", "\\\\)");    System.out.println(a);


It works properly. Then I have replace "$" with "\$". So I wrote
    String a = ":$";    a = a.replaceAll("\\$", "\\\\$");    System.out.println(a);


It throws a java.lang.StringIndexOutOfBoundsException.
Can anybody tell me why does this happen? Or tell me how to write a proper one to solve the job.
 

Posts:735
Registered: 11/07/01
Re: I need some help on regular expression  
Aug 6, 2004 12:58 AM (reply 1 of 2)



 
This way:
    String a = ":$";     a = a.replaceAll("\\$", "\\\\\\$");     System.out.println(a);


$ is to escape dollar char since it is used for matched group reference right? and \\\\ is the usual escape sequence for backward slash.
 

Posts:28
Registered: 6/13/01
Re: I need some help on regular expression  
Aug 6, 2004 2:40 AM (reply 2 of 2)



 
Thank you very much!
 
This topic has 2 replies on 1 page.