In a regular expression, match one thing or another, or both

前端 未结 5 1409
悲哀的现实
悲哀的现实 2020-11-28 14:35

In a regular expression, I need to know how to match one thing or another, or both (in order). But at least one of the things needs to be there.

For example, the fol

5条回答
  •  天涯浪人
    2020-11-28 14:38

    Nice answer by huon (and a bit of brain-twister to follow it along to the end). For anyone looking for a quick and simple answer to the title of this question, 'In a regular expression, match one thing or another, or both', it's worth mentioning that even (A|B|AB) can be simplified to:

    A|A?B
    

    Handy if B is a bit more complex.

提交回复
热议问题