1

My question is somewhat similar to this question. However, my question is different because of the following:

  • The question was not asked in a specific flavor.
  • It doesn't contain any of the regex pattern in JavaScript flavor, except one which only validates two level of nesting.
  • It only contain some algorithms, which I don't need because I'm trying to learn Regex.

I'm trying to learn Regex by validating an expression as "Balanced Parenthesis".

Balanced Parenthesis is an expression in which every ( is closed with a corresponding )

For example, the below expressions are valid:

(1+2)*(2+5)
(1+2(3+4)-6)/(2+4)
((()))

But, (3+4() is invalid.
For sake of simplicity, assume the terms inside the parenthesis to be digits and operators.


I'm using JavaScript regex. So, I tried the below regex. However, it doesn't worked out.

^(\((?:[^()]|\1*)\)[^()]*)+$

But, when I tried the below PCRE regex, it worked out nicely.

^(\((?:[^()]|(?1))*\)[^()]*)+$

The only syntactical difference in both the regex was how the first capturing group was re-used: \1 in JS and (?1) in PCRE. I know that \1 isn't a kind of subroutine, so are there any alternatives present in JavaScript, except repeating the whole pattern again?

I know a way using variables inside RegExp constructor. But, my regex is nested and so that one is ruled out.

EJoshuaS - Reinstate Monica
  • 10,460
  • 46
  • 38
  • 64
vrintle
  • 5,129
  • 1
  • 9
  • 39
  • 3
    JS regex engine does not support subroutines. – Wiktor Stribiżew Dec 12 '18 at 10:12
  • 1
    Altenatives all listed in the linked thread. See https://stackoverflow.com/a/25093078/3832970 and https://stackoverflow.com/a/35271017/3832970 – Wiktor Stribiżew Dec 12 '18 at 10:19
  • 2
    It is not possible to do with plain JS regex, and https://stackoverflow.com/a/546457/3832970 tells you that. – Wiktor Stribiżew Dec 12 '18 at 10:27
  • _It is not possible to do with plain JS regex_ - A single answer can't decide if it's possible or not. Let the others also get a chance to think over this, and _not simply accept that it is impossible_. I hope you'll understand me. – vrintle Dec 12 '18 at 10:34
  • 2
    However, that answer is correct for the current question. – Wiktor Stribiżew Dec 12 '18 at 10:43
  • You might want to tryout http://xregexp.com/ This is a recursive regex not supported by JS natively: https://regex101.com/r/sHt9fn/3 – Quasimodo's clone Dec 12 '18 at 10:46
  • 3
    @r7 *"Let the others also get a chance to think over this"* when a user with a [regex] gold badge and a [javascript] gold badge tells you it isn't possible with plain JS regex, it's imho not necessary to let other people give their input. – user247702 Dec 12 '18 at 11:10
  • 1
    Sure, we can reopen your question if you'd like. But know that the only outcome will be an answer with "it's not possible". I'll cast my reopen vote. – user247702 Dec 12 '18 at 11:17
  • Unfortunately, "it's impossible" *is* the answer. You might want to read about the [Chomsky hierarchy](https://en.wikipedia.org/wiki/Chomsky_hierarchy). – EJoshuaS - Reinstate Monica Dec 12 '18 at 14:17

1 Answers1

0

It is impossible to use a plain JS regex to match recursive structures of unknown depth.

There are non-regex implementations that let you match them in JS.

This is a duplicate question of Regular expression to match balanced parentheses.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397