Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

11
  • 24
    .NET's implementation has [Balancing Group Definitions msdn.microsoft.com/en-us/library/… which allow this sort of thing. Commented Jun 13, 2010 at 4:08
  • 50
    I disagree that regular expressions are the wrong tool for this for a few reasons. 1) Most regular expression implementations have a workable if not perfect solution for this. 2) Often you are trying to find balanced pairs of delimiters in a context where other criteria well suited to regular expressions are also in play. 3) Often you are handing a regular expression into some API that only accepts regular expressions and you have no choice. Commented May 2, 2014 at 3:31
  • 40
    Regex is the RIGHT tool for the job. This answer is not right. See rogal111's answer. Commented Dec 26, 2015 at 2:48
  • 12
    Absolutely agree with the answer. Although there are some implementations of recursion in regexp, they are equal to finite-state machines and are not supposted to work with nested structures, but Context Free Grammars do this. Look at Homsky's hierarcy of Formal Grammars. Commented Apr 20, 2016 at 10:52
  • 5
    Frank is right, Context free grammars cannot be described by regular expressions. That's the key point to this answer. Commented Jul 18, 2017 at 22:07