Regular expression matching any subset of a given set?

后端 未结 4 397
面向向阳花
面向向阳花 2021-01-13 14:38

Is it possible to write a regular expression which will match any subset of a given set of characters
a1 ... an ?
I.e. it should match any string whe

4条回答
  •  心在旅途
    2021-01-13 15:02

    This doesn't really qualify for the language-agnostic tag, but...

    ^(?:(?!\1)a1()|(?!\2)a2()|...|(?!\n)an())*$
    

    see a demo on ideone.com

    The first time an element is matched, it gets "checked off" by the capturing group following it. Because the group has now participated in the match, a negative lookahead for its corresponding backreference (e.g., (?!\1)) will never match again, even though the group only captured an empty string. This is an undocumented feature that is nevertheless supported in many flavors, including Java, .NET, Perl, Python, and Ruby.

    This solution also requires support for forward references (i.e., a reference to a given capturing group (\1) appearing in the regex before the group itself). This seems to be a little less widely supported than the empty-groups gimmick.

提交回复
热议问题