简体   繁体   中英

need a REGEX pattern to match

need help with validating a 9-digit number.

CANNOT BE 
-----------
000000000
111111111
222222222
333333333
444444444
555555555
666666666
777777777
888888888
999999999

4-5 position(s) CANNOT BE 00 -- 123001234

6-9 position(s) CANNOT BE 00 -- 234550000

The nine numbers CANNOT BE sequential -- but only the following 4 four below, for the time being --

012345678
123456789
987654321
098765432

I had just managed to get the first piece done --

"^(?:(?!0+|1+|2+|3+|4+))\d{9}$"

Thanks a TON for the help friends.

I realize it's not what was asked, but it strikes me that this would be easier as a standalone method, not a regex.

public bool SanityCheckSSN(string ssn)
{
    if(ssn == null || ssn.Length != 9) return false;

    foreach (char c in ssn)
    {
        if (!Char.IsDigit(c)) return false;
    }

    if(ssn == "000000000" ||
       ssn == "111111111" ||
       ssn == "222222222" ||
       ssn == "333333333" ||
       ssn == "444444444" ||
       ssn == "555555555" ||
       ssn == "666666666" ||
       ssn == "777777777" ||
       ssn == "888888888" ||
       ssn == "999999999" ||
       ssn == "012345678" ||
       ssn == "123456789" ||
       ssn == "987654321" ||
       ssn == "098765432" ||
       ssn.Substring(3, 2) == "00" ||
       ssn.Substring(5, 4) == "0000")
    {
        return false;
    }

    return true;
}

A single regex to validate all your rules may exist, but it will be much more easier, readable and maintainable if you write a validation rule (which can be a regex) for each of your criteria.

And as a more general comment, regex are usually great to check what a string IS, but they are not that good when it comes to check what a string IS NOT.

If it has to be a regex (written in verbose form):

^              # start of string
(?!(.)\1+$)    # not all characters the same
(?!...00)      # pos 4/5 not zero
(?!.....0000)  # pos 6-9 not zero
(?!012345678)  # list of disallowed numbers
(?!123456789)
(?!987654321)
(?!098765432)
[0-9]{9}       # match 9 digits exactly
$              # end of string
  • Same digit:
    ^(\\d)\\1*$

  • Groups of zeroes:
    ^\\d{3}00
    ^\\d{5}0*$

  • Sequences:
    ^0?123456789?$
    ^9?876543210?$

Putting it all together:

^(?!(\d)\1*$)
 (?!\d{3}00)(?!\d{5}0*$)
 (?!0?123456789?$)(?!9?876543210?$)
 \d{9}$

Everything but your "cannot be sequential" rule, which I don't understand.

^(?!(\d)\1+$)\d{3}(?!0{2})\d{2}(?!0{4})\d{4}$

Break-down:

^         # start-of-string
(?!       # negative look-ahead (not followed by)
  (\d)    #   a digit, store in group 1
  \1+$    #   the contents for group 1, repeated until end-of-string
)         # end negative look-ahead
\d{3}     # three digits
(?!       # negative look-ahead (not followed by)
  0{2}    #   two consecutive zeros 
)         # end negative look-ahead
\d{2}     # two digits
(?!       # negative look-ahead (not followed by) 
  0{4}    #   four consecutive zeros
)         # end negative look-ahead
\d{4}     # four digits
$         # end-of-string

That being said, the above is a good reason to use a dedicated function for this task instead of a regex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM