2

I was in a coding group and someone challenged us to write a regex to match everything outside of balanced brackets: https://i.stack.imgur.com/P2T34.png

The input:

Quiz: (Find all text that) outside of the outmost balanced parenthesis. (Be careful since) there might (be ((nested)) ones!) :) (async () => await console.log(1))()) You can do it! ()()(())():((1)((((((2)((3)))))))It would be even better the text is consecutive (and no more unneccessary (capturing groups!))() 2 * (1 + 3) = 8(eight) :( Did you do it ? (no) yes

The output:

Quiz: 
outside of the outmost balanced parenthesis. 
there might 
:)
...

I know it's not a "real" question to ask but I really want to know the answer. I searched online how to match the opposite: https://regex101.com/r/pr58AX/1 but I'm really curious about how to match everything except the brackets. Is it possible?

hiroshi
  • 461
  • 3
  • 8
  • You can still use the same regex but in a `regex.split` method. – Wiktor Stribiżew Jul 27 '23 at 12:26
  • @WiktorStribiżew Thanks but I don't think it is a programming language question, since in the screenshot they sent to me can match all the text directly – hiroshi Jul 27 '23 at 12:31
  • In PCRE you can use [`(*SKIP)(*F)`](https://stackoverflow.com/questions/24534782/how-do-skip-or-f-work-on-regex) to skip something. For your input sample something like this: [`(\((?:[^)(]+|(?1))*+\))(*SKIP)(*F)|[^)(]+`](https://regex101.com/r/rJ5DQ0/1) – bobble bubble Jul 27 '23 at 12:36
  • 2
    @bobblebubble I was checking the duplicate answer (somehow I didn't find it at the first place) and trying to change it so it would only work for only brackets`()`. This is so much cleaner! Thank you! – hiroshi Jul 27 '23 at 12:37
  • 2
    @bobblebubble That did not match `)` in `:)`. A slight modification: [`(\(([^()]|(?1))*+\))(*SKIP)(*FAIL)|(?:(?!(?1)).)+`](https://regex101.com/r/nXgbhh/2). – InSync Jul 27 '23 at 12:38

0 Answers0