Home > database >  regex to check if string doesn't contain non consecutive numbers on partial string
regex to check if string doesn't contain non consecutive numbers on partial string

Time:03-29

I am trying to create regex for below case:

  1. Input string consisting of all numbers, max length is 30.
  2. Check if in first 10 digits, any number is not consecutively appearing equal or more than 3 in length eg.
    1234567 --> is good (no consecutive number)
    1234456 --> is good (4 appears consecutive but length is less than 3)
    1234445 --> is bad (4 appears consecutive and length is equal or greater than 3)
    12345678904444 --> is good (4 appears consecutive and length is greater than 3 however it is accepted since it is appearing after cut off of 10 digit)

The regex I came up with is below. pardon me for my mistake if any in regex, i am still in learning mode with regexes: https://regex101.com/r/rv5e6a/1

currently it is getting applied all across the string but not sure how to limit so that regex can be applied only for first 10 digits only.

CodePudding user response:

^(?:(\d)(?!\1{2})){1,9}$|^(?:(\d)(?!\2{2})){10}\d*$

regex101 link

Explanation:

^              # beginning of the line
  (?:          # start of a non-capturing group
    (\d)       # a single digit in a group that we can refer to with \1 later on  
    (?!\1{2})  # not followed by the digit in the \1 group repeated twice
  ){1,9}       # repeat the non-capturing group 1-9 times  
$              # end of the line
|              # OR
^              # beginning of the line
  (?:          # start of a second non-capturing group 
    (\d)       # a single digit in a group that we can refer to with \2 later on      
    (?!\2{2})  # not followed by the digit in the \2 group repeated twice
  ){10}        # repeat the non-capturing group 10 times 
\d*            # the rest of the string can be more digits
$              # end of the line 

The important parts of the regex above makes sure that a given digit is not followed by the same digit two more times ^(?:(\d)(?!\1{2}). But, because we only care about the first 10 digits, we need to handle this in two cases.

  • in the first case, we have a string of digits that is less than 10 characters, so we want our pattern to repeat 1-9 times and then hit the end of the line.
  • in the second case, we have a string of digits that is 10 or more characters and then there might be even more characters after that that we don't care about.

We need to keep these two cases separate because we don't want to exclude the cases where there are fewer than 10 characters total in the string.

CodePudding user response:

You can use

^(?!\d{0,7}(\d)\1{2})\d{1,30}$

See the regex demo. Note that \d{0,7} in the lookahead will allow checking for repeated digits only within the first ten. More details:

  • ^ - start of string
  • (?!\d{0,7}(\d)\1{2}) - a negaitve lookahead that fails the match if there are three same digits after zero to seven digits immediately to the right of the current location
  • \d{1,30} - one to thirty digits
  • $ - end of string.
  • Related