[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [Regexp] How to use literal $ amounts in replace string for RE.subst
From: |
Jon Hoffman |
Subject: |
RE: [Regexp] How to use literal $ amounts in replace string for RE.substituteA ll(String,String) |
Date: |
Thu, 16 May 2002 23:41:10 -0700 |
Hi,
There's a provision for this in the API.
For instance you can do this:
str = re.substituteAll(str,"2 for $1.25", 0, RE.REG_NO_INTERPOLATE);
See the javadoc for more info.
Hth,
Jon
> -----Original Message-----
> From: address@hidden
> [mailto:address@hidden Behalf Of
> address@hidden
> Sent: Thursday, May 16, 2002 6:37 AM
> To: address@hidden
> Subject: [Regexp] How to use literal $ amounts in replace string for
> RE.substituteA ll(String,String)
>
>
> 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
>
>
>
>
> _______________________________________________
> Gnu-regexp-users mailing list
> address@hidden
> http://mail.gnu.org/mailman/listinfo/gnu-regexp-users