Regular expression matching A, B, and AB

僤鯓⒐⒋嵵緔 提交于 2020-01-02 08:03:40

问题


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 char
  • A? - an optional A
  • (?:B)? - an optional B.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!