please help me to validate balance brackets for string. my test string is below.
The industry [standard] for (transcribing) an audio ο¬le takes one hour for every ) 5 minutes of audio.
please help me to validate balance brackets for string. my test string is below.
The industry [standard] for (transcribing) an audio ο¬le takes one hour for every ) 5 minutes of audio.
The following regex is taken from Programming Ruby:
re = /
\A
(?<brace_expression>
{
(
[^{}] # anything other than braces
| # ...or...
\g<brace_expression> # a nested brace expression
)*
}
)
\Z
/x
\g<brace_expression>
here is used recursively.
Unfortunately, this isn't possible to determine using regular expressions. This is because regular expressions recognize regular languages, and nested patterns (which is essentially what you're asking for here) aren't part of regular languages.
"Can regular expressions be used to match nested patterns?" goes in to a little more detail on the subject.
Edit: Turns out Ruby's regex engine does support this. See Yu Hao's answer.