By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,377 Members | 3,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,377 IT Pros & Developers. It's quick & easy.

replaceAll and dollar sign and escaping?

P: n/a
Hello,

I've got a problem with the regular expression api. To illustrate the
problem i've made a little example:

Matcher m=Pattern.compile("test").matcher("this is a test not");
System.out.println(m.replaceAll("\\$_POST[]"));

Works fine, prints:

this is a $_POST[] not

Another example:

String str=readFromSource(); //contents of str is: \$_POST[]
Matcher m=Pattern.compile("test").matcher("this is a test not");
System.out.println(m.replaceAll(str));

This prints:

this is a \$_POST[] not

That's not good, cause the slash before the dollar sign shouldn't be
there.

I try:

String str=readFromSource(); //contents of str is: $_POST[]
Matcher m=Pattern.compile("test").matcher("this is a test not");
System.out.println(m.replaceAll(str));

That gives me a:

java.lang.IllegalArgumentException : Illegal group reference
How can i get output the following output?

this is a $_POST[] not

Best regards,
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Ding <ie******@hotmail.com> wrote in message news:<vt********************************@4ax.com>. ..
Hello,

I've got a problem with the regular expression api. To illustrate the
problem i've made a little example:

Matcher m=Pattern.compile("test").matcher("this is a test not");
System.out.println(m.replaceAll("\\$_POST[]"));

Works fine, prints:

this is a $_POST[] not

Another example:

String str=readFromSource(); //contents of str is: \$_POST[]
Matcher m=Pattern.compile("test").matcher("this is a test not");
System.out.println(m.replaceAll(str));

This prints:

this is a \$_POST[] not

That's not good, cause the slash before the dollar sign shouldn't be
there.

I try:

String str=readFromSource(); //contents of str is: $_POST[]
Matcher m=Pattern.compile("test").matcher("this is a test not");
System.out.println(m.replaceAll(str));

That gives me a:

java.lang.IllegalArgumentException : Illegal group reference
How can i get output the following output?

this is a $_POST[] not

Best regards,


You don't need to use regexp API directly for simple replacement.
String tgt = "this is a test not";
System.out.println(tgt.replaceAll("test", "\\$_POST[]"));
Jul 17 '05 #2

P: n/a
Take a look at String.replaceAll in the Java API. The '$' character
doesn't need to be escaped in a String, since it is considered a
normal character in that respect. So for your second example, the
contents of str should have just been $_POST[].

As for replaceAll/regex, the '$' is considered "end of contents", so
that "foo$" will match on 'barfoo' and 'quuxfoo', but not 'foobar'. \$
means, no, forget about it, I just mean the '$' character.

That being said, \$ is considered a single character to Java (like
\n), so you need to double-escape it to get it right:
\\\$ = a real $-sign in a regex

Michael Scovetta

Ding <ie******@hotmail.com> wrote in message news:<vt********************************@4ax.com>. ..
Hello,

I've got a problem with the regular expression api. To illustrate the
problem i've made a little example:

Matcher m=Pattern.compile("test").matcher("this is a test not");
System.out.println(m.replaceAll("\\$_POST[]"));

Works fine, prints:

this is a $_POST[] not

Another example:

String str=readFromSource(); //contents of str is: \$_POST[]
Matcher m=Pattern.compile("test").matcher("this is a test not");
System.out.println(m.replaceAll(str));

This prints:

this is a \$_POST[] not

That's not good, cause the slash before the dollar sign shouldn't be
there.

I try:

String str=readFromSource(); //contents of str is: $_POST[]
Matcher m=Pattern.compile("test").matcher("this is a test not");
System.out.println(m.replaceAll(str));

That gives me a:

java.lang.IllegalArgumentException : Illegal group reference
How can i get output the following output?

this is a $_POST[] not

Best regards,

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.