Regex for no duplicate characters from a limited character pool

前端 未结 3 491
时光取名叫无心
时光取名叫无心 2021-01-04 20:55

Is there a way to write a regex to match a string that only contains certain characters, and never repeats those characters? I already wrote some code using a set to impleme

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 21:36

    I'm answering this question

    Visual C++ Regex: Matching no repeating letter

    which was marked an exact duplicate of this question.

    No repeating of any letter ?
    Does that mean consecutive letters, or is abaca ok ?

    if not, the regex would be yours modified to this :

    ^(?=.*[a-z])(?!.*([a-z]).*\1).{4,20}$

    Expanded

     ^ 
     (?= .* [a-z] )
     (?!
          .* 
          ( [a-z] )                     # (1)
          .* \1 
     )
     .{4,20} 
     $ 
    

提交回复
热议问题