Poornima Prakash
Poornima Prakash

Reputation: 330

Escape ( in regular expression

Im searching for the regular expression - ".(conflicted copy.". I wrote the following code for this

String str = "12B - (conflicted copy 2013-11-16-11-07-12)";
boolean matches = str.matches(".*(conflicted.*");
System.out.println(matches);

But I get the exception

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed group near index 15 .(conflicted.

I understand that the compiler thinks that ( is the beginning of a pattern group. I tried to escape ( by adding \( but that doesnt work.

Can someone tell me how to escape ( here ?

Upvotes: 2

Views: 274

Answers (3)

Maroun
Maroun

Reputation: 95978

Escaping is done by \. In Java, \ is written as \\1, so you should escaping the ( would be \\(.

Side note: It's good to have a look at Pattern#quote that returns a literal pattern String. In your case, it's not that helpful since you don't want to escape all special-characters.


1 Because a character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler.

Upvotes: 7

Pshemo
Pshemo

Reputation: 124265

( in regex is metacharacter which means "start of group" and it needs to be closed with ). If you want refex engine to tread it as simple literal you need to escape it. You can do it by adding \ before it, but since \ is also metacharacter in String (used for example to create characters like "\n", "\t") you need to escape it as well which will look like "\\". So try

str.matches(".*\\(conflicted.*"); 

Other option is to use character class to escape ( like

str.matches(".*[(]conflicted.*"); 

You can also use Pattern.quote() on part that needs to be escaped like

str.matches(".*"+Pattern.quote("(")+"conflicted.*"); 

Or simply surround part in which all characters should be threaded as literals with "\\Q" and "\\E" which represents start and end of quotation.

str.matches(".*\\Q(\\Econflicted.*"); 

Upvotes: 2

Niels Keurentjes
Niels Keurentjes

Reputation: 41968

In Regular Expressions all characters can be safely escaped by adding a backslash in front.

Keep in mind that in most languages, including C#, PHP and Java, the backslash itself is also a native escape, and thus needs to be escaped itself in non-literal strings, so requiring you to enter "myText \\(".

Using a backslash inside a regular expression may require you to escape it both on the language level and the regex level ("\\\\"): this passes "\\" to the regex engine, which parses it as "\" itself.

Upvotes: 1

Related Questions