[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Regexp] How to use literal $ amounts in replace string for RE.substitut
From: |
Jonathan . Cogley |
Subject: |
[Regexp] How to use literal $ amounts in replace string for RE.substituteA ll(String,String) |
Date: |
Thu, 16 May 2002 09:37:04 -0400 |
All,
I have looked through the past mailing lists but couldn't find any mention of
this issue so I apologize in advance if it has come up before.
I am using the following code:
String str = "The socks are: <price/>";
try {
RE re = new RE("<price/>");
System.out.println(str);
str = re.substituteAll(str,"2 for $1.25");
System.out.println(str);
} catch (REException ree) {
System.out.println("An REException occurred.");
System.out.println(ree.getMessage());
ree.printStackTrace();
}
Output:
The socks are: <price/>
The socks are: 2 for .25
I understand that the $1 represents a sub expression in my regexp which is ""
because I don't define a sub expression. I tried escaping the $1 as \$1 but
still the same results. Tracing through the source to
REMatch.substituteInto(String) I noticed that the code does not check for any
form of escaping around $digit values in the replace string.
1) How do I escape the dollar value as a literal string?
2) Out of curiosity, why does gnu.regexp use $1,$2,$3,etc when Perl uses
\1,\2,\3 for sub expressions?
TIA,
Jonathan
- [Regexp] How to use literal $ amounts in replace string for RE.substituteA ll(String,String),
Jonathan . Cogley <=