0

When I try:

Pattern p = Pattern.compile("[,.s;:{}/[]<>?`~!@#$%^&*()_+=]");

my program bugs out. Why doesn't it like this?

2
  • You mean by crash that it is throwing an exception ? What does that exception say ? Commented Nov 6, 2013 at 16:33
  • Maybe you are missing an import ? Commented Nov 6, 2013 at 16:33

4 Answers 4

3

This regular expression won't compile because in Java you need to escape square brackets [, ] when you use them inside character classes:

Pattern p = Pattern.compile("[,.s;:{}/\\[\\]<>?`~!@#$%^&*()_+=]");
                                      ^^^^^^

The double escape \\ is needed because slashes \ are used in Java strings to escape special sequences like \n, \r ... etc

Now how do we include a literal slash in a Java string when we need one if it is used to escape stuff ?

We escape it using it self thus typing it twice \\.

Why do we need to escape [ and ] inside character classes ?

Because Java supports character class subtraction, intersection and union, for example:

[a-d[m-p]]  a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]]    d, e, or f (intersection)
[a-z&&[^bc]]    a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]]   a through z, and not m through p: [a-lq-z](subtraction)

Examples are taken from the documentation.

Sign up to request clarification or add additional context in comments.

4 Comments

Why the double escape?
@AprilRandolph Because the backslash needs to be escaped too!
@AprilRandolph I have edited my answer to cover your comment.
@AprilRandolph If this question solves your problem then you should accept it by clicking the tick mark to the left :) Thank you.
2

You need to escape special characters such as [, ], +, (, ) etc. I'm not 100% sure but you might be able to use \Q and \E to tell the regex to treat the special chars as literals.

Eg:

Pattern p = Pattern.compile("[\\Q,.s;:{}/[]<>?`~!@#$%^&*()_+=\\E]");

See the Quotation section in the javadoc

2 Comments

Also since you are using quotation [\\Q\\[\\]\\E] escaping [ and ] is incorrect because it will make \, [, \, ] literals in character class and OP don't want to include \ in this class.
Hmm... it seems I copy / pasted a regex from another answer instead of the the regex from the original question. Fixed.
0

This is correct regex:

Pattern p = Pattern.compile("[,.s;:{}/\\[\\]<>?`~!@#$%^&*()_+=]");

You need to escape [ and ]

OR this will also work:

Pattern p = Pattern.compile("[],.s;:{}/\\[<>?`~!@#$%^&*()_+=]");

With only [ needs to be escaped.

] can avoid escaping if it is at first position inside character class.

Comments

0

As already said, you have to skip the special characters ... In order to do that I will suggest you to use the Pattern.quote method (see here as a reference).

String s = Pattern.quote("[,.s;:{}/[]<>?`~!@#$%^&*()_+=]");
Pattern p = Pattern.compile(s);

Comments

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.