Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to check for 4 consecutive numbers

Tags:

regex

Can I use

\d\d\d\d[^\d]

to check for four consecutive numbers?

For example,

411112 OK

455553 OK

1200003 OK

f44443 OK

g55553 OK

3333 OK

f4442 No

45553 No

f4444g4444 No

f44444444 No

like image 376
Oh Chin Boon Avatar asked Apr 24 '12 08:04

Oh Chin Boon


People also ask

How do I check a number in regex?

The [0-9] expression is used to find any character between the brackets. The digits inside the brackets can be any numbers or span of numbers from 0 to 9. Tip: Use the [^0-9] expression to find any character that is NOT a digit.

How do I match a pattern in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .

Can regex be used for numbers?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. That's the easy part. Matching the three-digit numbers is a little more complicated, since we need to exclude numbers 256 through 999.

What is the meaning of \\ in regular expression?

Backslash is escape character for the next backslash in front of it. Question mark means optional. So optional backslash. Sabhya Kaushal.


Video Answer


3 Answers

If you want to find any series of 4 digits in a string /\d\d\d\d/ or /\d{4}/ will do. If you want to find a series of exactly 4 digits, use /[^\d]\d{4}[^\d]/. If the string should simply contain 4 consecutive digits use /^\d{4}$/.

Edit: I think you want to find 4 of the same digits, you need a backreference for that. /(\d)\1{3}/ is probably what you're looking for.

Edit 2: /(^|(.)(?!\2))(\d)\3{3}(?!\3)/ will only match strings with exactly 4 of the same consecutive digits.

The first group matches the start of the string or any character. Then there's a negative look-ahead that uses the first group to ensure that the following characters don't match the first character, if any. The third group matches any digit, which is then repeated 3 times with a backreference to group 3. Finally there's a look-ahead that ensures that the following character doesn't match the series of consecutive digits.

This sort of stuff is difficult to do in javascript because you don't have things like forward references and look-behind.

like image 194
Daan Avatar answered Sep 30 '22 15:09

Daan


Should the numbers be part of a string, or do you want only the four numbers. In the later case, the regexp should be ^\d{4}$. The ^ marks the beginning of the string, $ the end. That makes sure, that only four numbers are valid, and nothing before or after that.

like image 31
mistalee Avatar answered Sep 30 '22 17:09

mistalee


That should match four digits (\d\d\d\d) followed by a non digit character ([^\d]). If you just want to match any four digits, you should used \d\d\d\d or \d{4}. If you want to make sure that the string contains just four consecutive digits, use ^\d{4}$. The ^ will instruct the regex engine to start matching at the beginning of the string while the $ will instruct the regex engine to stop matching at the end of the string.

like image 34
npinti Avatar answered Sep 30 '22 17:09

npinti