You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by peter reilly <pe...@corvil.com> on 2003/09/05 11:22:57 UTC

Re: [Ant-contrib-developers] propertyregex: '\' within patterns

The reason is (fairly) simple.

The cause is that \digit is used
as a replacement token.

So the code in 
org.apache.tools.ant.util.regex.(JakartaOro|Jdk14Regexp|JakartaRegexp)Regexp
#substitute
check if a \ appears in the string and if so if the next
character is a digit, if so it then does the subs (for oro
and 14 this means placing a $digit in the character stream).
If the following character is not a digit, it gets
put in the stream, in other words the backslash is dropped.

As \ is also an escape character for the oro/14/jakarata matcher 
function it needs to be excaped again.

You need to place two in the output stream so you
need 2 * 2 * 2 or 8 backslashs.

7 backslashs work if the last backslash is the last character
in the replacement string, in which change the code
commented by /* XXX - should throw an exception instead? */
comes into play.

I am not to sure if this (dropping the \ character if it
is not followed by a digit) is correct behaviour, however
it has been in ant for a while, so needs to be maintained
for BC reasons.

Peter.

On Thursday 04 September 2003 18:29, Rutger Hofman wrote:
> Why do I have to quadruplate the backslash? Why in replace and
> not in regexp?
> I would expect, to replace '\' with '\\':
>    regexp="\\" replace="\\\\"             (2 and 4)
> or if I must quadruplate, then I would expect
>    regexp="\\\\" replace="\\\\\\\\"       (4 and 8)
>
> It is like none of these. Specifying 8 '\' in replace also
> gives the correct result. So, 2 and 8 is OK, as is 2 and 7.
> However, 4 and 8 gives a failure to match the pattern:
> 4 apparently matches 2 '\' in the input string.
>
> (In fact, I would not have been surprised if the seventh
> backslash would have escaped the following end-quote, but ant
> life seems not so simple.)
>
> Greetz,
>
> Rutger Hofman
> VU Amsterdam
>
> Inger, Matthew wrote:> Backslashes should be in increments of two if you're
>
> > actually trying to represent the backslash character.
> > So, you should probably have 8 backslashes.
> >
> > ----------------
> > Matthew Inger [inger@synygy.com]
> > Software Developer
> > Synygy, Inc
> > 610-664-7433 x 7770
> > "Man who jump off cliff, leap to conclusions." - Confucious
> >
> >
> > -----Original Message-----
> > From: Rutger Hofman [mailto:rutger@cs.vu.nl]
> > Sent: Thursday, September 04, 2003 12:13 PM
> > To: ant-contrib-developers@lists.sourceforge.net
> > Subject: [Ant-contrib-developers] propertyregex: '\' within patterns
> >
> >
> > Another question about propertyregex.
> >
> > I want to basically set property p-out from replacing
> > "\" with "\\" in some property, p-in. Now, '\' is of
> > course escape character, so I must replace it. I would
> > presume that regexp="\\" and replace="\\\\" would do
> > the job.
> >
> > However, I must do:
> >
> > <propertyregex
> > 	property="p-out"
> > 	input="${p-in}"
> > 	regexp="\\"
> > 	replace="\\\\\\\"
> > 	global="true" />
> >
> > This is, yes, seven(!) backslashes in replace,
> > where I would expect four.
> >
> > Other numbers of backslashes in replace lead to
> >
> > java.lang.StringIndexOutOfBoundsException: String index out of range: 3
> >          at java.lang.String.charAt(String.java:460)
> >          at java.util.regex.Matcher.appendReplacement(Matcher.java:544)
> >
> > or other errors.
> >
> > Any clue? Is this going to be required (and documented) behaviour,
> > or is it a bug?
> > If you want, I can spend some time to hunt down the bug (if my professor
> > allows it :-)
> >
> > Rutger
> >
> >
> >
> >
> > -------------------------------------------------------
> > This sf.net email is sponsored by:ThinkGeek
> > Welcome to geek heaven.
> > http://thinkgeek.com/sf
> > _______________________________________________
> > Ant-contrib-developers mailing list
> > Ant-contrib-developers@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/ant-contrib-developers
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Ant-contrib-developers mailing list
> Ant-contrib-developers@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ant-contrib-developers


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org