In these two patterns:
1: abc)
2: a(bc)
I am trying to create a pattern that will match string 1 and return NULL if matched against string 2. I.e. only match if one right parenthesis is present with no left parenthesis. If both a right and a left parenthesis is present, then return NULL. So I am trying:
~/ % pcretest
PCRE version 8.45 2021-06-15
re> "(.*?)\((*SKIP)(*F)|.*?\)"
data> "abc)"
0: "abc)
data> "a(bc)"
0: bc)
The expression does skip the left parenthesis in "a(bc), but I would like it to return NULL. How can I do that?
CodePudding user response:
Using a recursive pattern, you can match a )
when there are unbalanced parenthesis.
(\((?:[^()] |(?1))*\))(*SKIP)(*F)|\)
(
Capture group 1\(
Match(
(?:[^()] |(?1))*
Optionally repeat matching any char except(
and)
or repeat the first sub pattern (group 1)\)
Match)
)
Close group 1(*SKIP)(*F)
Skip the match|
Or\)
Match a single)
CodePudding user response:
Converting my comment to answer so that solution is easy to find for future visitors.
This problem doesn't need to use fancy PCRE feature as it can be solved using standard regex with a negated character class (assuming we are not onto complex cases of nested or escaped parentheses):
^[^(]*\)
Explanation:
^
: Start[^(]*
: Match 0 or more of any characters except(
\)
: Match right)