Match repeated digits from number - regex

I need regex to check numbers for repeated digits.
All numbers contain 12 digits, first 6 digits we need to skip, so I need to find numbers where every second digit from 7 repeated.
Like this 964632X5X7X3 X - repeated digits
Results
502632959793 - TRUE
125632757773 - TRUE
475632353773 - FALSE
I have try something like this for every digits from 0 to 9:
\d{6}([9]\d[9]\d[9]\d)$
It didnt work.

You may use
^\d{6}(?=(\d))(?:\1\d){3}$
See the regex demo. You may even refactor this regex later if you need to accommodate any x to y amount of repetitions after the first six digits (just replace {3} with the required {x}, {x,} or {x,y} quantifier with the required thresholds).
Regex details
^ - start of string
\d{6} - the first six digits
(?=(\d)) - a positive lookahead that captures the seventh digit into Group 1
(?:\1\d){3} - three occurrences of the digit captured in Group 1 and any single digit
$ - end of string

Related

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

I am trying to create regex for below case:
Input string consisting of all numbers, max length is 30.
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.
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.
^(?:(\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.

Regex expression for numbers and leading zeros just with a dot and decimal

I'm trying to find a regex for numeric inputs. We can receive a leading 0 just if we add a dot for adding 1 or 2 decimal numbers. And of course just accept numbers.
These are the scenarios that we can accept:
0.01
1.1
1.02
120.01
We can't accept these values
0023
0100
.01
.12
Which regex is the best option for these cases?
Until now we try we the following regex for accepting just number and dots
[A-Za-z,]
And also we try with the following ones:
^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{1})?|(?:,[0-9]{3})*(?:\.[0-9]{1,2})?|(?:\.[0-9]{3})*(?:,[0-9]{1,2})?)$
"/^[-]?[$]\d{1,3}(?:,?\d{3})*\.\d{2}$/"
"/(^(\d{1})\.{0,1}([0-9]){0,2}$)|(^([1-9])\d{0,2}(\,\d{0,3})$)/g"
(?:0|[1-9][0-9]*)(?:\.[0-9]{1,2})?
And the next one for deleting the leading zeros but it didn't work for 0.10 cases
^0+
If a negative lookahead is supported, you can exclude matches that start with a zero and have no decimal part.
^(?!0\d*$)\d+(?:\.\d{1,2})?$
^ Start of string
(?!0+\d*$) Negative lookahead, assert not a zero followed by optional digits at the right
\d+ Match 1+ digits
(?:\.\d{1,2})? Match an optional decimal part with 1 or 2 digits
$ End of string
Regex demo
I would go with ^(0|[1-9]\d*|(0|[1-9]\d*)\.\d+)$
You can test here: https://regex101.com/r/oNMgR9/1
Explanation
^ means : match the beginning of the string (or line if the m flag is enabled).
$ means : match the end of the string (or line if the m flag is enabled).
(a|b) means match "a" or match "b" so I'll use this to match either "0" alone or any number not starting with a "0". It's the syntax for a logical or.
. alone is used to match any char. So you have to escape it if you want to match the dot character. This is why I wrote 0\. instead of 0..
[ ] is used to list some characters you want to match. It can be a range if you use the - char, so [1-9] means any digit char from "1" to "9".
\d is to match a digit. It's totally equivalent to [0-9].
* means : match the preceding pattern 0 or many times, so \d* means that it will match 0 or many times a digit, so it will match "8" or "465" or "09" but also an empty string "". If you want to match the preceding pattern at least once or many times then you use + instead of *. So \d+ won't match an empty string "" but \d* would match it.
A) Just a number not starting with 0
[1-9]\d* will match any digit from 1 to 9 and then optionnaly followed by other digits. This will match numbers without a decimal point.
B) Just 0
0 alone is a possibility. This is because the case above isn't covering it.
B) A number with decimals
(0|[1-9]\d*)\.\d+ will match either a "0" alone or a number not starting by "0" and then followed by a point and some other digits (which have to be present because we don't want to match "45." without the numbers behind the dot).
Better alternative
The solution from #TheFourthBird is a bit cleaner with the use of a negative lookahead. It's just a bit different to understand. And he read the question completely: You wanted 1 or 2 digits after the decimal. I forgot about that, so, effectively, \d+ should be replaced by \d{1,2} as you don't want more than 2 digits.
You can use
^(?![0.]+$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$
See the regex demo.
Details:
^ - start of string
(?![0.]+$) - fail the match if there are just zeros or dots till end of string
(?:[1-9]\d*|0) - either a non-zero digit followed with any zero or more digits or a zero
(?:\.\d{1,2})? - optionally followed with a sequence of a . and one or two digits
$ - end of string.

REGEX to check if a number is repeated in a string

I need a regex that check if a number is repeated in a string of 9 char, knowing that the chars could be an indefinite number of dots
Examples
"........." - false - no numbers repeated
"123456789" - false - no numbers repeated
"1.2.3.4.5" - false - no numbers repeated
"1...3...5" - false - no numbers repeated
"112345678" - true - number 1 is repeated
"1......1." - true - number 1 is repeated
"11244.56." - true - number 1 and 4 are repeated
"234.5.6.4" - true - number 4 is repeated
I found this looking around on internet that is close to what i need
\b(?:([1-9])(?![1-9]*\1)){1,9}\b
But I don't know how to make it not consider dots
Thanks very much :)
You can use
^(?=.{9}$)(?=.*(\d).*\1)(?:\.*\d){1,9}\.*$
Or, if you need to only allow digits from 1 to 9:
^(?=.{9}$)(?=.*([1-9]).*\1)(?:\.*[1-9]){1,9}\.*$
See this regex demo.
Details:
^ - start of string
(?=.{9}$) - the string should contain 9 chars
(?=.*(\d).*\1) - there must be a repeating digit
(?:\.*\d){1,9}\.* - 1 to 9 occurrences of any zero or more dots followed with a digit and then any zero or more dots
$ - end of string.
This regexp will allow any characters between the repeated digits:
(\d).*\1
\d matches a digit, () puts it in capture group 1, and the back-reference \1 matches the same digit.

Regex compare whole number and ignore separators

I would like to match the following pattern
1.XXXXXX.XXX.X
The combination must begin with a 1 and must contain at least a second number greater than 0 somewhere. Only numbers and points allowed.
So the following examples would be correct
1.000000.000.1
1.000500.000.0
1.020030.030.0
And the following examples would be incorrect
1.000000.000.0
1.0000.00.0
1.0000d0.020.0
What I have created so far
(?=^[1][\.][0-9]{6}[\.][0-9]{3}[\.][0-9]{1}$) // check pattern 1.XXXXXX.XXX.X
(?=^[1-9](?!0000000000$)[0-9][1-9]?\d+$) // check if input is greater than 10000000000
Unfortunately, the second statement does not work because of the points in the input. Is it possible to read the complete number and ignore the points or is there a better solution?
You may use
^1\.(?!(?:\.?0)+$)\d{6}\.\d{3}\.\d$
See the regex demo
The regex will fail the match if there are only zeros and dots after the initial 1..
Details
^1\. - 1. at the start of the string
(?!(?:\.?0)+$) - a negative lookahead that will fail the match of there are one or more sequences of an optional . and a zero up to the end of the string
\d{6} - 6 digits
\. - a dot
\d{3} - 3 digits
\. - a dot
\d - a digit
$ - end of string.

Regex limiting a number string

I am trying to figure out how to use regex to pass a 6 digit number string. My trouble is the string can be any 6 digits, unless it starts with 12. So the first digit can be 1 but not if second digit is 2. The second digit can be 2, but not if the first is 1.
I tried this, ([^1])([^2])(\d{4}) but that does not take into account both digits, so it will block anything with a 2 in the second spot.
Thank you for any help.
You may use
^([02-9][0-9]|[0-9][013-9])[0-9]{4}$
See the regex demo
Details:
^ - start of string
([02-9][0-9]|[0-9][013-9]) - either of the two alternatives:
[02-9][0-9] - any digit but 1 and then any digit
| - or
[0-9][013-9] - any digit and then any digit but 2
[0-9]{4} - any 4 digits
$ - end of string.
Another way is to use a negative lookahead:
^(?!12)[0-9]{6}$
See another demo. Here, (?!12) fails the match if the first 2 digits are 12. The [0-9]{6} will match 6 digits.
Depending on the regex library/method, ^/$ anchors may not be required. Lookaheads are not always supported, too.