问题
I would like to create a regular expression that matches A
, B
, and AB
, where A
and B
are quite complex regular expressions.
One solution is to use (A|A?B)
or (AB?|B)
, but then I have to repeat one of the expressions.
A?B?
does not work, since this also matches the empty string.
Is it possible to create this regular expression without repeating neither A
nor B
?
回答1:
Edit
You may add a lookahead to require at least 1 char:
(?=.)A?(?:B)?
^^^^
See the regex demo. If you need to support linebreaks, use '~(?=.)A?(?:B)?~s'
or, without a modifier, (?=[\s\S])A?(?:B)?
.
Details
(?=.)
- immediately to the right, there must be 1 char other than linebreak charA?
- an optionalA
(?:B)?
- an optionalB
.
Original answer
Yes, you will have to use alternation, but in PCRE, you may recurse subpatterns with subroutine calls:
A(?<BGroup>B)?|(?&BGroup)
See the regex demo.
The (?<BGroup>B)
is a named capturing group whose pattern is repeated with the (?&BGroup)
named subroutine call.
See Recursive patterns.
回答2:
I would go for storing A and B into variables and create the pattern (AB?|B) from A and B by concatenation. This has the advantage of enhancing readability as you can document the subpatterns A, and B.
来源:https://stackoverflow.com/questions/43863404/regular-expression-matching-a-b-and-ab