Hello.
I tried the program below expecting an output like "To Phrase."
public class ToPhrase {
public static void main(String[] args) {
String s = "ToPhrase".replaceAll("(.)([A-Z])", "\1 \2");
System.out.println(s);
}
}
But the real output was broken. 2nd byte was 0x01 and 4th byte was 0x02.
Other bytes were normal ASCII characters.
What happened?
Thank you in advance.
Kiwi
> I tried the program below expecting an output like "To Phrase."
>
[quoted text clipped - 8 lines]
> Other bytes were normal ASCII characters.
> What happened?
Two things.
The backslash character is a special character in Java, so you have to
escape it with a backslash, if you don't you end up using the octal
escape syntax.
In Java the "capturing group n" syntax can only be used in the pattern,
not in the replacement text (and remember to escape the backslash).
You can accomplish what you are trying to do
with look-behind / look-ahead
public class ToPhrase {
public static void main(String[] args) {
String s = "ToPhrase".replaceAll("(?<=.)(?=[A-Z])", " ");
System.out.println(s);
}
}
Kiwi - 13 Jan 2005 07:11 GMT
> The backslash character is a special character in Java
Thank you. I was stupid.
Kiwi
Thomas Schodt - 12 Feb 2005 21:52 GMT
> In Java the "capturing group n" syntax can only be used in the pattern,
> not in the replacement text (and remember to escape the backslash).
Correction.
<http://forum.java.sun.com/thread.jspa?threadID=595916>