TallChuck TallChuck - 1 month ago 12
Python Question

Python Regex that will work for any type of bracket

Is there a way to take a regular expression, such as

\(.*\)


and make it correctly identify pairs of any type of bracket, like

(\(|\{|\[).*(\)|\}|\])


without making incorrect matches, like
\(.*\]
?

I'm specifically working with Python, but it should work similarly in any language.

Answer

No. Regular languages can't handle nesting correctly. You'll need a proper parser for that.