-4

() [] {} these are valid matches but ({)} is false, eg.:

() - true
()[]- true
()[]{} - ture
([]) - false
(){[}] - false.

Can I use regular expression to compute this?

mellamokb
  • 56,094
  • 12
  • 110
  • 136
sethukrs
  • 19
  • 1
  • 2
  • 2
    Use a stack. It'll make your head hurt much less. – Makoto Apr 19 '13 at 21:57
  • 1
    You asked an almost identical question here http://stackoverflow.com/questions/16113504/java-best-way-to-implement-this-code – peter.murray.rust Apr 19 '13 at 21:57
  • What a strangely positioned question, did you hear someone say that regex cannot be used to balance parenthesis? – Esailija Apr 19 '13 at 21:58
  • @peter.murray.rust: I don't see a duplication. That was more broad than this question (which is still broad, mind you). – Makoto Apr 19 '13 at 21:58
  • possible duplicate of [Can regular expressions be used to match nested patterns?](http://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns) – Makoto Apr 19 '13 at 21:59
  • @Makoto: Nevermind that they are by the same user :/ – mellamokb Apr 19 '13 at 21:59
  • @mellamokb: "Best way" vs. "Is this possible with...". That's what I was looking at. – Makoto Apr 19 '13 at 21:59
  • No, you're going to want to look in to `Context-free grammars` and `deterministic pushdown automata` http://en.wikipedia.org/wiki/Context-free_grammar#Well-formed_nested_parentheses_and_square_brackets – Michael Apr 19 '13 at 22:01
  • @peter.murray.rust yeah but do you have a clue ??? – sethukrs Apr 19 '13 at 23:20

1 Answers1

5

No, one of known limitations of regular expressions is that it can't tell whether parentheses are well formed.