Regular expression exclude double character

前端 未结 2 1388
借酒劲吻你
借酒劲吻你 2020-12-11 19:27

I need to have regular expression which find a character which not following by same character after it. Mean exclude double even multiple same character.

For exampl

2条回答
  •  失恋的感觉
    2020-12-11 20:09

    /\b([a-df-z]*e[a-df-z]*)\b\s*/g
    

    You could add the flag case insensitive /i if needed.

    Explanation:

    /               : regex delimiter
      \b            : word boundary
      (             : start group 1
        [a-df-z]*   : 0 or more letter that is not "e"
        e           : 1 letter "e"
        [a-df-z]*   : 0 or more letter that is not "e"
      )             : end group 1
      \b            : word boundary
      \s*           : 0 or more spaces
    /g              : regex delimiter, global flag
    

    As you didn't give which language you're using, here is a perl script:

    my $str = "need only single characteeer";
    my @list = $str =~  /\b([a-df-z]*e[a-df-z]*)\b\s*/g;
    say Dumper\@list;
    

    Output:

    $VAR1 = [
              'single'
            ];
    

    And a php script:

    $str = "need only single characteeer";
    preg_match_all("/\b([a-df-z]*e[a-df-z]*)\b\s*/", $str, $match);
    print_r($match);
    

    Output:

    Array
    (
        [0] => Array
            (
                [1] => single 
            )
    
        [1] => Array
            (
                [1] => single
            )
    
    )
    

提交回复
热议问题