-1

I need to validate the a regex string as I get as parameter does not contain more than one capturing groups.

How can I do that with regex?

Tried counting occurrences of this \(.*?\)

but I need to somehow add the option to not count parentheses as caputre groups: (:?*)

Can anybody help me generate a better string?

I know that there are posts on validating regex by regex, but here I need specific validation for big query that does not support two capturing groups on regex_exreact. I don't need to validate that the regex is correct.


UPDATE:.

I tried both suggestions here: \((?!\?).*?\)? [^\\]?\([^?]

they almost work.

Apparently, after testing, I found out that capturing group within a capturing group is valid.

for example: ^aaa \\s?(\\([a-z]+\\))?$ is valid Can this be done?

Thanks

Ido Barash
  • 4,856
  • 11
  • 41
  • 78
  • 2
    Do you mean not count _non-capturing_ groups? Maybe this `\((?!\?).*?\)`? Note that you can't use regex to fully validate a regex pattern though. – 41686d6564 stands w. Palestine Feb 28 '21 at 11:51
  • Maybe relevant: [Is there a regular expression to detect a valid regular expression?](https://stackoverflow.com/q/172303) – VLAZ Feb 28 '21 at 11:53
  • Does this answer your question? [Is there a regular expression to detect a valid regular expression?](https://stackoverflow.com/questions/172303/is-there-a-regular-expression-to-detect-a-valid-regular-expression) – Ryszard Czech Feb 28 '21 at 23:12
  • Apparently capturing group within a capturing group is allowed. Can this be done? – Ido Barash Mar 01 '21 at 09:40
  • The given post does not answer my question - added reason in the description. – Ido Barash Mar 01 '21 at 09:46

1 Answers1

1

Like someone commented: You can't validate a regular expression with a regular expression.

But you can do this:

[^\\]?\([^?]

This will match any ( that is not escaped (\(), or is followed by a ?. Which is essentially any capturing group.

cochaviz
  • 126
  • 6