Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String replaceAll() and regex question

Reply
Thread Tools

String replaceAll() and regex question

 
 
grasp06110@yahoo.com
Guest
Posts: n/a
 
      05-27-2006
Hi All,

I would like to hand the replaceAll() method of String text that it
would interpret as literal text and not as a regex. In the code I am
trying to implement I am given two variables and a string and need to
replace one variable with the second in the string. If either of the
variables contains a special character (e.g. $) the replaceAll() method
can fail. An example of this is shown below. In the example below I
can escape the $ character with /$ but I don't want to have to search
each string for every potential trouble maker. I would like to tell
the regex to treat the entire string as a literal.

Any help would be appreciated.

Thanks,
John


public class Ouch {

public static void main(String[] args) throws Exception {
String start = "yours now for just <amount>";
String price = "$9.99";
String end = start.replaceAll("<amount>", price);
System.out.println(end);
}

}

Exception in thread "main" java.lang.IndexOutOfBoundsException: No
group 9
at java.util.regex.Matcher.group(Matcher.java:463)
at java.util.regex.Matcher.appendReplacement(Matcher. java:730)
at java.util.regex.Matcher.replaceAll(Matcher.java:80 6)
at java.lang.String.replaceAll(String.java:2000)
at Ouch.main(Ouch.java:7)

 
Reply With Quote
 
 
 
 
Martin Gerner
Guest
Posts: n/a
 
      05-28-2006
wrote in news:1148773195.171205.166250@
38g2000cwa.googlegroups.com:

> I would like to hand the replaceAll() method of String text that it
> would interpret as literal text and not as a regex.


What you probably want to use is actually String.replace(). The name is
unfortunate - it replaces more than one occurence, even if it is easy to
believe otherwise by just seeing the name (together with replaceAll()). see
the API for more info.

http://java.sun.com/j2se/1.5.0/docs/...g.html#replace
(char,%20char)


--
Martin Gerner
 
Reply With Quote
 
 
 
 
Martin Gerner
Guest
Posts: n/a
 
      05-28-2006
Martin Gerner <> wrote in
news:Xns97D119D348608martindotgerneratnos@129.16.2 22.141:

> wrote in news:1148773195.171205.166250@
> 38g2000cwa.googlegroups.com:
>
>> I would like to hand the replaceAll() method of String text that it
>> would interpret as literal text and not as a regex.

>
> What you probably want to use is actually String.replace(). The name
> is unfortunate - it replaces more than one occurence, even if it is
> easy to believe otherwise by just seeing the name (together with
> replaceAll()). see the API for more info.
>
> http://java.sun.com/j2se/1.5.0/docs/...g.html#replace
> (char,%20char)
>
>


Hm, the link got bogged up. Well, just scroll down until you get on
replace.

--
Martin Gerner
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-28-2006
<> wrote:
> I would like to hand the replaceAll() method of String text that it
> would interpret as literal text and not as a regex. In the code I am
> trying to implement I am given two variables and a string and need to
> replace one variable with the second in the string. If either of the
> variables contains a special character (e.g. $) the replaceAll() method
> can fail.


Yep. See the static methods Pattern.quote and Matcher.quoteReplacement.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
g.rajeshchowdary@gmail.com
Guest
Posts: n/a
 
      05-28-2006
Hi John!!
I am not sure why this is not working. But have you tried this with
StringBuffer?

wrote:
> Hi All,
>
> I would like to hand the replaceAll() method of String text that it
> would interpret as literal text and not as a regex. In the code I am
> trying to implement I am given two variables and a string and need to
> replace one variable with the second in the string. If either of the
> variables contains a special character (e.g. $) the replaceAll() method
> can fail. An example of this is shown below. In the example below I
> can escape the $ character with /$ but I don't want to have to search
> each string for every potential trouble maker. I would like to tell
> the regex to treat the entire string as a literal.
>
> Any help would be appreciated.
>
> Thanks,
> John
>
>
> public class Ouch {
>
> public static void main(String[] args) throws Exception {
> String start = "yours now for just <amount>";
> String price = "$9.99";
> String end = start.replaceAll("<amount>", price);
> System.out.println(end);
> }
>
> }
>
> Exception in thread "main" java.lang.IndexOutOfBoundsException: No
> group 9
> at java.util.regex.Matcher.group(Matcher.java:463)
> at java.util.regex.Matcher.appendReplacement(Matcher. java:730)
> at java.util.regex.Matcher.replaceAll(Matcher.java:80 6)
> at java.lang.String.replaceAll(String.java:2000)
> at Ouch.main(Ouch.java:7)


 
Reply With Quote
 
grasp06110@yahoo.com
Guest
Posts: n/a
 
      05-28-2006
Hi,

Martin Gerner's suggestion of using String.replace looks like it should
work. My bad for not looking at the api more closely.

Thanks!
John

 
Reply With Quote
 
Bernd Klier
Guest
Posts: n/a
 
      05-29-2006
wrote:
> Hi All,


Hi

>
> public class Ouch {
>
> public static void main(String[] args) throws Exception {
> String start = "yours now for just <amount>";
> String price = "$9.99";
> String end = start.replaceAll("<amount>", price);
> System.out.println(end);
> }
>
> }
>
> Exception in thread "main" java.lang.IndexOutOfBoundsException: No
> group 9


You have to escape the '$' character. Try
String price = "\\$9.99";
and it'll work for you.

HTH

Bernd
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regex =~ string or string =~ regex? Ruby Newbee Ruby 3 01-04-2010 06:04 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57