473,383 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

replaceAll and dollar sign and escaping?

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
2 21296
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Mark Hahn | last post by:
We are considering switching to the dollar sign ($) for self, instead of the period ( . ) we are using now in Prothon. Ruby uses the at-sign (@) for self, but our new usage of self also includes...
3
by: mark.a.lemoine | last post by:
I need to be selecting records based on the value of a string-format currency value. Specifically, I have a table with a field of type varchar(50). Stored in this field is a dollar-formatted...
17
by: Digital Puer | last post by:
I've inherited some code where the coder placed dollar signs in his preprocessor macros. What is the significance of the dollar signs ($) ? Example: #define ALLOCATE(task,pointer) \ { \...
2
by: Yorian | last post by:
I just started to try regexps in php and I didn't have too many problems, however I found a few when trying to build a templte engine. The first one is found is the dollar sign. In my template I...
2
by: johnson4 | last post by:
Im wanting to display a dollar sign on my wap application, which requires that the cell phone is served with $$ to display $. Therefor I'm trying to find an easy way, in my php code, that will...
1
by: Energizer100 | last post by:
In the code below, I'm trying to convert a sentence into shorthand, meaning I take away all the vowels, plus I convert "and" to "&", "to" to "2", "you" to "U", and "for" to "4". public static...
14
by: gimme_this_gimme_that | last post by:
What is going on here with the dollar signs and parenthesis? $(document).ready(function(){ $("li").behavior("click",function(){ $(this).load(menu.html"); }); }); And when do you use click...
29
by: Nick | last post by:
I've seen a few frameworks use the following: function $(id) { return document.getElementById(id); } Then to use: $('something').innerHTML = 'blah'; I'm just trying to roll this out to my...
4
by: Robbie Hatley | last post by:
Some months ago, I'd asked this group: "Backtick, at-sign, dollar-sign: legal in source?" I got 20 replies, most of which were off-topic and did not answer the question. Keith Thompson...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.