I am trying to parse and, if necessary, replace the contents of a String object.
public class StringTest {
public static void main(String[] args) {
final String FRONT_SLASH_PRODUCTION = "/production";
final String BACK_SLASH_PRODUCTION = "\\production";
String input = "/production/wheeeeeee"; //sample String
System.err.println(input.replaceAll(FRONT_SLASH_PRODUCTION, "/build"));
System.err.println(input.replaceAll(BACK_SLASH_PRODUCTION, "\\build"));
}
}
Running the above results in the following output:
/build/wheeeeeee
java.util.regex.PatternSyntaxException: Unknown character category {r} near index 2
\production
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.familyError(Unknown Source)
at java.util.regex.Pattern.retrieveCategoryNode(Unknown Source)
at java.util.regex.Pattern.family(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceFirst(Unknown Source)
Exception in thread "main"
Instead of just escaping the '\', Java seems to be expecting a character category to follow the slash-slash-p (the forum software seems to interpret two front slashes as a newline). I took some time to look at category codes, and would think that there needs to be an opening brace after '\p' (e.g. \p{L}) in order for it to expect a category code. Is there a way around this? I tried to set BACK_SLASH_PRODUCTION to "\\p{L}roduction", and it incorrectly changed "/production/wheeeeeee" to "/build/wheeeeeee". It also changed "/prroduction/wheeeeeee" to "/pbuild/wheeeeeee". If it helps, I'm working in 1.4.2_13-b06.
Thanks