Regular Expression for matching parentheses

211,119

Solution 1

Two options:

Firstly, you can escape it using a backslash -- \(

Alternatively, since it's a single character, you can put it in a character class, where it doesn't need to be escaped -- [(]

Solution 2

The solution consists in a regex pattern matching open and closing parenthesis

String str = "Your(String)";
// parameter inside split method is the pattern that matches opened and closed parenthesis, 
// that means all characters inside "[ ]" escaping parenthesis with "\\" -> "[\\(\\)]"
String[] parts = str.split("[\\(\\)]");
for (String part : parts) {
   // I print first "Your", in the second round trip "String"
   System.out.println(part);
}

Writing in Java 8's style, this can be solved in this way:

Arrays.asList("Your(String)".split("[\\(\\)]"))
    .forEach(System.out::println);

I hope it is clear.

Solution 3

  • You can escape any meta-character by using a backslash, so you can match ( with the pattern \(.
  • Many languages come with a build-in escaping function, for example, .Net's Regex.Escape or Java's Pattern.quote
  • Some flavors support \Q and \E, with literal text between them.
  • Some flavors (VIM, for example) match ( literally, and require \( for capturing groups.

See also: Regular Expression Basic Syntax Reference

Solution 4

For any special characters you should use '\'. So, for matching parentheses - /\(/

Solution 5

Because ( is special in regex, you should escape it \( when matching. However, depending on what language you are using, you can easily match ( with string methods like index() or other methods that enable you to find at what position the ( is in. Sometimes, there's no need to use regex.

Share:
211,119

Related videos on Youtube

Ammu
Author by

Ammu

Updated on July 05, 2022

Comments

  • Ammu
    Ammu almost 2 years

    What is the regular expression for matching '(' in a string?

    Following is the scenario :

    I have a string

    str = "abc(efg)";
    

    I want to split the string at '(' using regular expression.For that i am using

    Arrays.asList(Pattern.compile("/(").split(str))
    

    But i am getting the following exception.

    java.util.regex.PatternSyntaxException: Unclosed group near index 2
    /(
    

    Escaping '(' doesn't seems to work.

    • Harry Joy
      Harry Joy about 13 years
      You should first learn basics of Regex: Wiki Regex or Regex Tuts or Regex for begginer
    • Kobi
      Kobi about 13 years
      Try Pattern.quote("("), or simply "\\(". Note that there are 2 backslashes - extra one for the Java compiler, to understand the string correctly. Your example code is using a slash, not a backslash. More importantly, you can simple use string.split...
    • someone
      someone about 4 years
      "\(" worked for me, thanks to @Kobi.
  • Totem
    Totem almost 9 years
    An explanation as to what is going on here would improve this answer. If the OP didn't know to do this in the first place, chances are they won't understand your solution either. In general, answers consisting of just code, or just links are not good answers
  • snakedoctor
    snakedoctor almost 2 years
    ***** YES !!!!!!!!!!! I am doing some python and this one definitely worked for me, thank you. In some languages like Java and Python you have to use two '\' characters before the (