-1

please help me to validate balance brackets for string. my test string is below.

The industry [standard] for (transcribing) an audio file takes one hour for every ) 5 minutes of audio.
jayesh
  • 2,422
  • 7
  • 44
  • 78
  • 1
    Welcome to Stack Overflow. Unfortunately, Stack Overflow isn't a code-writing service. Instead, we help you debug code you wrote. Please show us the code you've tried and we'll be happy to help you fix it. As is, your question hasn't met the basic requirements and is off-topic, so please add that code. – the Tin Man Jan 12 '15 at 19:21

2 Answers2

3

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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
2

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.

Community
  • 1
  • 1
joost
  • 129
  • 2
  • 9
  • That's true for common regex, but don't forget Ruby's regex engine supports many extensions. – Yu Hao Jan 12 '15 at 13:26
  • 1
    Good one, I hadn't taken that into account (actually learned this from your post :) ) – joost Jan 12 '15 at 15:39