0

I have the following Regex: [<>\\]|\p{C}

How can I create as String with that in Java, the problem is in the Regex are two backslashes:

String reg="[<>\\]|\p{C}";   // this gets error  

Pattern p = Pattern.compile(reg);
2
  • 1
    What are your imput strings and what would you like the result to be? When asking regex questions - give a few examples on input and output Commented Nov 9, 2015 at 19:30
  • Escape a backslash with another backslash ... Commented Nov 9, 2015 at 19:32

1 Answer 1

4

Just escape the backslash, with a backslash :

String reg="[<>\\\\]|\\p{C}";
Sign up to request clarification or add additional context in comments.

2 Comments

Your first regex was correct. When you write regexes in the form of Java string literals, you always have to use four backslashes to match one. Metacharacters like * lose their special meanings inside character classes, but escape sequences like \s don't, so the backslash is always special.
Btw, you can just include the Unicode property in the character class "[\\p{C}<>\\\\]"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.