0

Is it possible to match parenthesis like () but not allowing nesting? In other words, I want my regex to match () but not (()) The regex that I am trying is

\(\[^\(\)])

but it does not seem to be working. Can someone explain to me what I'm doing wrong?

5
  • 4
    Once you start dealing with "state" (am I in nested parens?) in your parser, regular expressions often fail to meet your demands. Commented Aug 4, 2015 at 20:15
  • 3
    Do you want it to match (()? How about ())? Do you need to match other contents of parantheses, like (foo)? If so, what about (foo (bar) baz)? Commented Aug 4, 2015 at 20:31
  • @fredrikhl I want the regex to match (foo) but neither ((foo)) nor (foo (bar) baz) Commented Aug 4, 2015 at 20:42
  • Do you want to match the (foo) in ((foo)) and the (bar) in (foo (bar) baz), or are partial matches unacceptable for these strings? Commented Aug 4, 2015 at 20:54
  • @das-g I dont want partial matches. In other words, only (foo) is acceptible. Commented Aug 4, 2015 at 20:56

1 Answer 1

1

If (foo) in x(foo)x shall be matched, but (foo) in ((foo)) not, what you want is not possible with regular expressions, as regular expressions represent regular grammars and all regular grammars are context free. But context (or 'state', as Jonathon Reinhart called it in his comment) is necessary for the distinction between the (foo) substrings in x(foo)x and ((foo)).

If you only want to match strings that only consist of a parenthesized substring, without any parentheses (matched or unmatched) in that substring, the following regex will do:

^\([^()]*\)$
  • ^ and $ 'glue' the pattern to the beginning and end of the string, respectively, thereby excluding partial matches
  • note the arbitrary number of repetitions (…*) of the non-parenthesis character inside the parentheses.
  • note how special characters are not escaped inside a character set, but still have their literal meaning. (Putting backslashes in there would put literal backslashes in the character set. Or in this case out of the character set, due to the negation.)
  • note how the [ starting the character set isn't escaped, because we actually want its special meaning, rather than is literal meaning

The last two points might be specific to the dialect of regular expressions Python uses.

So this will match () and (foo) completely, but not (not even partially) (foo)bar), (foo(bar), x(foo), (foo)x or ()().

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

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.