$s1='afa';
$s2='bebeeeb';
$s1=~/((\w)(?!\2))+\2?/;
此正则表达式匹配两个字符串。 我想只匹配第一个字符串。 (第一个字符后跟任何字符,但不是第一个字符。捕获的两个字符可以重复任意次。)
答案 0 :(得分:0)
答案 1 :(得分:0)
更容易检查相反的情况。
# Doesn't contain repeated word characters.
$s !~ /(\w)\1/
否则,您必须在每个位置检查 。 (你在任何位置检查 。)
# Every character is a non-word character or a non-repeated word character
$s =~ /^(?:\W|(\w)(?!\1))*\z/
如果输入只能包含单词字符,则上述内容简化为
# Every character is a non-repeated word character
$s =~ /^(?:(\w)(?!\1))*\z/
甚至
# Every character is a non-repeated character
$s =~ /^(?:(.)(?!\1))*\z/s