Home > other >  How can I make a regular expresion that verify if a opening parenthesis has its closing parenthesis?
How can I make a regular expresion that verify if a opening parenthesis has its closing parenthesis?

Time:12-09

I've got a question. I have this regular expresion:

const r = /((\B[\(])*(\w) (\b[\)])*(\s)*) /g;

But lets suppose that we have the string :

ball (doll) (bicycle

So we wanna verify if every opening parenthesis has its closing parenthesis, then I wanna that this string returns me a false value, because the substring "(bicycle" hasn't its closing parenthesis.

What can I do to create a pattern with regexp that verifies this? THANKS!! :D

CodePudding user response:

You can try this :

(?<=\()[^(] ?(?=\))

You can see the demo here with code comments

CodePudding user response:

I assume that parentheses are not nested. I also assume that the intent of the question to confirm that parentheses are matched (or "balanced"). For example, ')))))(cat))' does not have matched parentheses but "every opening parenthesis [here just one] has its closing parenthesis".

There are at least a couple ways to do this. The conventional way is to match

^(?:[^()]*\([^()]*\))*[^()]*$

Demo

The elements of this regular expression are as follows.

^         # match the beginning of the string
(?:       # begin a non-capture group
  [^()]*  # match 0  characters other than left and right parentheses
  \(      # match a left parenthesis
  [^()]*  # match 0  characters other than left and right parentheses
  \)      # match a right parenthesis
)*        # end non-capture group and execute it 0  times
  [^()]*  # match 0  characters other than left and right parentheses
$         # match end of string

Another way is to recognize that parentheses are balanced if all of the following are true:

  • no substring delimited by right parentheses that contains no other right parenthesis contains a left parenthesis
  • no substring delimited by left parentheses that contains no other left parenthesis contains a right parenthesis
  • the first parenthesis in a string (if there is one) is not a right parenthesis
  • the last parenthesis in a string (if there is one) is not a left parenthesis

We can implement these four requirements with four negative lookaheads:

^(?!.*([()])[^()]*\1)(?![^()]*\))(?!.*\([^()]*$)

Demo

The elements of this regular expression are as follows.

^         # match beginning of string
(?!       # begin a negative lookahead
  .*      # match 0  characters
  ([()])  # match a left or right parenthesis and save to capture group 1 
  [^()]*  # match 0  characters other than left and right parentheses
  \1      # match the content of capture group 1
)         # end negative lookahead
(?!       # begin a negative lookahead
  [^()]*  # match 0  characters other than left and right parentheses
  \)      # match a right parenthesis
)         # end negative lookahead
(?!       # begin a negative lookahead
  .*      # match 0  characters
  \(      # match a left parenthesis
  [^()]*  # match 0  characters other than left and right parentheses
  $       # match end of string
)         # end negative lookahead

You can also hover your cursor over each element of each of these regular expressions at the links to obtain an explanation of the function of the element.

  • Related