I need help with a regex that should match a fixed length pattern.
For example, the following regex allows for at most 1 (
and 1 )
in the matched pattern:
([^)(]*\(?[^)(]*\)?[^)(]*)
However I can not / do not want to use this solution because of the *
, as the text I have to scan through is very large using it seems to really affect the performance.
I thus want to impose a match length limit, e.g. using {10,100}
for example.
In other words, the regex should only match if
- there are between 0 and 1 set of parentheses inside the string
- the total length of the match is fixed, e.g. not infinite (No
*
!)
This seems to be a solution to my problem, however I do not get it to work and I have trouble understanding it. I tried to use the accepted answer and created this:
^(?=[^()]{5,10}$)[^()]*(?:[()][^()]*){0,2}$
which does not seem to really work as expected: https://regex101.com/r/XUiJZz/1
I am unable to make use of the kleene star operator.
Edit
I know this is a possible solution, but I'm wondering if there is a better way to do it:
([^)(]{0,100}\(?[^)(]{0,100}\)?[^)(]{0,100})