2011-12-14 141 views
0

我的电话号码验证当前正则表达式是^\d{8,}$这验证最小长度为8,不允许特殊字符或字母和我的问题是什么是不允许连续9-15正则表达式的电话号码重复号码加当前条件(最小长度为8,不允许特殊字符或字母)。正则表达式不连续的重复号码

谢谢...

回答

0

所以11111111(8个1 S)是好的,但111111111(9个1 S)是不是?

一个正则表达式是不是总是正确的答案。我会保持现有的^\d{8,}$正则表达式,然后分别检查重复数字。 (?现在),因为你只禁止10个不同的数字,你可以只设立禁号码的哈希和对证:

my %forbidden = map { $_ x 9 => 1 } 0..9; 

... 

if ($num =~ /^\d{8,}$/ and not $forbidden{$num}) { 
    # accept 
} 
else { 
    # reject 
} 
1

如果我正确理解您的需求,这个正则表达式将做到这一点:

/^(?!.*(\d)\1{8})\d{8,}$/ 

这里是一个注释版本(在C#语法):

Regex regexObj = new Regex(@" 
    # Match digits sequence with no same digit 9 times in a row. 
    ^    # Anchor to start of string 
    (?!.*(\d)\1{8}) # Assert no digit repeats 9 times. 
    \d{8,}   # Match eight or more digits. 
    $    # Anchor to end of string", 
    RegexOptions.IgnorePatternWhitespace); 
1

要了解以更好的方式正则表达式

NODE说明

^      the beginning of the string 
    (?!      look ahead to see if there is not: 
    .*      any character except \n (0 or more times 
         (matching the most amount possible)) 
    (      group and capture to \1: 
     \d      digits (0-9) 
    )      end of \1 
    \1{8}     what was matched by capture \1 (8 times) 
    )      end of look-ahead 
    \d{8,}     digits (0-9) (at least 8 times (matching 
         the most amount possible)) 
$      before an optional \n, and the end of the 
         string 
+0

感谢您对我的不断扩大表达的意见,以更好地描述行径,在超前内。它高兴地看到另一个“关注到细节” regexpert在这里! +1 – ridgerunner 2011-12-26 16:24:39