Regex to check non-repetition of a set of characters

前端 未结 8 939
无人共我
无人共我 2020-12-19 12:47

Suppose I have the set of characters [ABC]. I\'m looking for a regex that would match any permutation of the superset except the empty set, i.e.



        
8条回答
  •  悲哀的现实
    2020-12-19 13:15

    "A((B?C?)|(C?B?))|B((A?C?)|(C?A?))|C((A?B?)|(B?A?))"
    

    It is A|B|C and each of them can be followed by an pair of optional values

     A(B?C?) matches A, AB,AC and ABC
     A(C?B?) matches A, AC,AB and ACB 
    

    but not ACAC, AA or ACC. The cases with B or C as first character are equivalent.

    For longer Strings, this will get ugly soon. A better approach would be (pseudocode):

     string.sort().matches ("^A?B?C?$") && string.length > 0
    

提交回复
热议问题