Regex for no duplicate characters from a limited character pool

前端 未结 3 492
时光取名叫无心
时光取名叫无心 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} 
     $ 
    
    0 讨论(0)
  • 2021-01-04 21:45

    That's easy to do with a negative lookahead assertion:

    ^(?!.*(.).*\1)[ABC]+$
    

    matches exactly as you described.

    Test it live on regex101.com.

    Explanation:

    ^      # Start of the string
    (?!    # Assert that it's impossible to match...
     .*    # Any number of characters (including zero)
     (.)   # followed by one character (remember this one in group 1)
     .*    # that's followed by any number of characters
     \1    # and the same character as before
    )      # End of lookahead
    [ABC]+ # Match one or more characters from this list
    $      # until the end of the string
    
    0 讨论(0)
  • 2021-01-04 21:49

    back referencing can be used. Here comes an example in PHP, which is compatible to Perl regular expressions:

    $string = "A, B, C, AB, AC, B, BC, AABC";
    
    if(preg_match('/([ABC])\1/', $string, $matches)) {
        echo $matches[1] . " has been repeated\n";
    } else {
        echo "OK\n";
    }
    

    In the above pattern ([ABC]) is capturing group which can store one out of the characters A, B or C. \1 references this first capturing group, this makes the pattern matching if one those characters repeats.

    0 讨论(0)
提交回复
热议问题