AnkurVj
AnkurVj

Reputation: 8198

Matching Parantheses Using a regular expression

I am trying to write a regular expression to match any string that satisfies the following criteria.

The string begins and ends with a matching pair of parentheses '(' ')'

There may be any number of parentheses within it.

For example my regex shud match :

( ( p(x)+q(x) ) . (p(x) * q(x) ) )

but not match

( p(x)+q(x) ) . ( p(x) * q(x) )

How do i write such a regex

Upvotes: 1

Views: 464

Answers (2)

Kelly S. French
Kelly S. French

Reputation: 12334

Doing any sort of parsing like this using regular expressions is difficult and almost always a bad idea. See this answer to this question. Oh, the horror!

Upvotes: 1

Related Questions