Regular expression matching A, B, and AB
问题 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