Can Python Regex Count Parentheses?
I am wondering if python regular expressions can count. For example, would they have the ability to skip over nested parentheses? I know Java cannot do this so I am wondering if py
Solution 1:
Basic regexes are strictly defined as what can be expressed with a finite state automaton. No regex can do this. Thus in general regexes cannot use counters. This is one of the consequences of the Pumping Lemma as @chrisaycock pointed out. Regardless of the programming language (and the language to express Regular expressions), it is impossible...
Regexes are however capable of enumerating over all matches. One can thus count the number of open (
and closing )
brackets. But is not able to verify if they match the guidelines of brackets (no closing bracket where there was no open bracket, etc.)
For purposes like nested brackets, context-free grammars exist, like PLY.
Post a Comment for "Can Python Regex Count Parentheses?"