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

前端 未结 5 1392
悲哀的现实
悲哀的现实 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:42

    Maybe this helps (to give you the general idea):

    (?:((?(digits).^|[A-Za-z]+)|(?\d+))){1,2}
    

    This pattern matches characters, digits, or digits following characters, but not characters following digits. The pattern matches aa, aa11, and 11, but not 11aa, aa11aa, or the empty string. Don't be puzzled by the ".^", which means "a character followd by line start", it is intended to prevent any match at all.

    Be warned that this does not work with all flavors of regex, your version of regex must support (?(named group)true|false).

提交回复
热议问题