regex expression in javascript - regex

I am trying to build a regex in javascript to match a 9-digit number with these characteristics:
First 3 digits should not be ‘0’ ,
4th and 5th Digit should not be ‘0’,
Last 4 digits should not be ‘0000’,
First 3 digits should not be ‘666’,
Last 3 Digits Should not be greater than ‘899’
Can someone please help me out with this.
Here is my current regex:
/^666[^0]{3}[1-9]{2}0000$/
, but it’s not meeting the criteria

Try This regular expression it will work ^(?!(000)|(666))[0-9]{3}[1-9]{2}[0-9][0-8][0-9]{2}(?<!0000)$. See demo here

Try this ^[^06]{3}[^0]{2}[^0][1-8][^0]{2}$
Below is my explanation for it
First 3 digits should not be ‘0’
First 3 digits should not be ‘666’,-> ^[^06]{3}
4th and 5th Digit should not be ‘0’, -> [^0]{2}
Last 4 digits should not be ‘0000’, -> [^0]{4}
Last 3 Digits Should not be greater than ‘899’ -> [1-8][^0]{2}$

Because all nine characters need to be a digit, you might use lookahead from the beginning to check that there are 9 digits followed by the end of the string, which will make the subsequent groups easier to manage. Then, you need to utilize character sets and negative lookahead. The first two conditions look to collapse together - the first five characters need to be other than 0:
/^(?=\d{9}$)(?!666)[^0]{5}(?!0000)\d[^9]/
const re = /^(?=\d{9}$)(?!666)[^0]{5}(?!0000).[^9]/;
`555555555
5555555555
055555555
555505555
666555555
555550000
555550001
555550900
555550953`
.split('\n')
.forEach(n => console.log(re.test(String(n))));
Explanation:
555555555 true
5555555555 false; 10 digits, not 9
055555555 false: has 0 in first 5 digits
555505555 false: has 0 in first 5 digits
666555555 false: starts with 666
555550000 false: ends with 0000
555550001 true
555550900 false: sixth digit is a 9 (so last 3 digits are 9xx, which is greater than 899)
555550953 false: same as above
https://regex101.com/r/Vpwbk0/1

Related

Write regex patterns for matching single digit or double digit where tens place value is 2 or 4

Below is my regex for matching 2 digit where tens place value is 2 or 3 and it is working fine.
^(?=[2,4])\d{1,2}$
As soon as I add the regex for matching single digit in above regex , It started matching single digit and as well all 2 digit number.
^(?=\d|[2,4])\d{1,2}$
I want below sample input to be matched.
0
1
2
3
24
44
48
29
28
Below not to be matched.
99
11
33
55
77
Also It will great help if I would get to know why my regex is not working.
You get a difference in matches as the positive lookahead asserts that there must be to the right what you specify. In there first pattern that is either 2 4 or , and in the second case just a single digit.
You don't have a comma in your example data, so in that case you can match an optional 2 or 4 using just [24]? followed by a digit without any lookarounds.
^[24]?\d$
See a regex demo.
Try this: ^(\d|[2,4]\d)$
Test regex here: https://regex101.com/r/aZo7fK/1
^(\d|[2,4]\d)$
^ matches the start of string
(\d|[2,4]\d) matches either a single digit(0-9) or a two digit number which starts with either 2 or 4
$ matches the end of the string
This matches either a single digit(0-9) number or a two digit number which starts with either 2 or 4.
I suggest
^[2,4]?[0-9]$
pattern; where
^ - anchor, start of the text
[2,4]? - optional 2 or 4 digit for tens
[0-9] - mandatory digit 0..9 for units
$ - anchor, end of the text
Edit: Now, let's have a look at your current patterns; the first is
^(?=[2,4])\d{1,2}$
Here
(?=[2,4]) - look ahead for 2 or 4
\d{1,2} - one or two digits
as we can see 3 doesn't match: look ahead fails to find 2 or 4. As for your second attempt
^(?=\d|[2,4])\d{1,2}$
pattern, where
(?=\d|[2,4]) - look ahead for ANY digit (note, that |[2,4] is redundant)
\d{1,2} - one or two digits
the pattern matches too many texts; technically it matches any one or two digit numbers, e.g. for:
79
we have
(?=\d|[2,4]) - look ahead - succeeds with 7
\d{1,2} - one or two digits - succeeds with 79

Regex match 2 letters and up to 10 digits afterwards

I have the following regex which is matching the first 2 letters RR and then 4 numbers after.
RR[0-9]{4}
How can I change it to detect the first 2 letters RR and then up to 10 digits afterwards?
I know I can do...
^[0-9]*$
To match all numbers but how can I limit this and add it to the first regex?
You can use RR\d{0,10}. This matches RR, followed by 0 to 10 digits, i.e. up to 10 digits.

Iranian postal code validation

Please, I need to validate Iranian postal code using regex.
I write this regex for this case \b([^02\n\D]){4}[^5](\d){5} but its not working on rule number 5 and 7.
please help me to fix it.
this is some rules about this regex:
It's all numeric
10 digit count
don't use 0 in first 5 digit
don't use 2 in postal code
First 4 digit is not the same
The 5th digit cannot be 5
all digits aren't the same
The following regex satisifes your conditions:
\b(?!(\d)\1{3})[13-9]{4}[1346-9][013-9]{5}\b
Click for Demo
Explanation:
\b - a word boundary
(?!(\d)\1{3}) - negative lookahead to make sure that the 1st 4 digits are not the same.
[13-9]{4} - matches 4 occurrences of all the digits except 0 and 2
[1346-9] - matches a single digit that is not a 0,2 or 5
[013-9]{5} - matches 5 occurrences of all the digits except 2
\b - a word boundary

Regex Match range numbers between 13000 to 99999

I am trying to write some form validation, I need one of the inputs to be 13000-99999.
(^[1-1][3-3]?[0-9]?[0-9]?[0-9]?$|^[0-9][0-9][0-9][0-9][0-9]$)
It does not work as expected and it match all the following :
10 \\ matched but it should not
10000 \\ matched but it should not
12999
13000
20000
99999
can anyone help me? Thanks!
Although the way you are doing this is not ideal. But if you are doing with this approach, your regex needs some changes:
(^[1-1][3-9][0-9][0-9][0-9]$|^[2-9][0-9][0-9][0-9][0-9]$)
This is because if the 1st digit is a 1, then the second number should be between a 3 and a 9. If the 2nd digit is a 2, then any of 0-9 is valid for the second digit. The last three digits are always 0-9 range.
You have more than one issue here
(^[1-1][3-3]?[0-9]?[0-9]?[0-9]?$|^[0-9][0-9][0-9][0-9][0-9]$)
1 - You have to remove all question mark ?
2 - In the second pattern in the first part [3-3] should be from [3-9]
3 - In the second part after | this should be from [2-9]
You can use one of the following regex (^[1-1][3-9][0-9][0-9][0-9]$|^[2-9][0-9][0-9][0-9][0-9]$), (^[1-1][3-9]|^[2-9][0-9])[0-9][0-9][0-9]$ or 1[3-9]\d{3}|[2-9]\d{4}
first you need to match any number that start from 1 and from 3 to 9 or start from 2 to 9 and any number 0 to 9 and all rest number can any from 0 to 9

Can a Regex be written to find numbers in a string greater than x?

Can a Regex be written to find numbers in a string that are greater than x?
Say for example x = 1800
Can we find for that a number > 1800 which is in the strings: "3,000.00/month" | "$2800" | "150,000.00a month" | "only $1900" etc? The regex would find 3,000.00, 2800, 150,000.00 and 1900 because they are all greater than 1899.
What is this pattern that elludes me..
You can find any well-formatted number greater than a number with a regular expression, but it does get more complicated as the numbers get bigger.
Let's start with a simple example where x = 11, and you wanted to find any number in a string greater than 11. The regex you'd write would follow a pattern similar to the solution below:
(\d{3,}|[2-9]\d|1[2-9])(\.\d+)?|11\.\d*[1-9]\d*
REY
What is the purpose of first three inner alternations? The first captures any three or more digit number, the second any two digit number where the digit in the 10's place is 2-9, and the last any two digit number where the digit in the 10's place is 1 and the digit in the 1's place is 2-9. Then it optionally captures any decimal decimal digits.
The second outer alternation actually matches x in the integer part, but then checks the decimal part to ensure it is somehow greater. If you had a decimal part of 12 for x then you'd just do 120*\d*|1[3-9]\d*|2\d*.
There are two situations that will make the above pattern not work:
When a number contains commas
When a number starts with zeroes.
The example below deals with those cases as well and solves for x = 1800.
#FIND any number > 1800
(?=[1-9]) # ensure number doesn't begin with a zero
(
(\d{1,3},(?=\d{3}))*\d{2,3},\d{3}| # any number that has >= 5 digits with commas
\d{5,}| # any number that has >= 5 digits without commas
[2-9],?\d{3,}| # any 4 digit number that starts with 2-9
1,?9\d{2}| # any 4 digit number that starts with 1 then 2-9
1,?8[1-9]\d| # any 4 digit number that starts with 18 then 1-9
1,?80[1-9] # any 4 digit number that starts with 180 then 1-9
)
(\.\d+)? # any decimal digits
|1,?800\.\d*[1-9]\d* # any number whoses integer = 1800,
# ... then has a decimal with a non-zero digit.
REY
At the top of the regex is uses a look ahead to ensure a number actually starts with 1-9. Without it a number like 00005 would be confused as a five digit number.
The inner group finds all integer matches greater than 1800, just like the first example tried to match numbers greater than eleven. Probably, the only non obvious alternation is the first - (\d{1,3},(?=\d{3}))*\d{2,3},\d{3}. To find a 5+ digit number it matches 1-3 digits followed by a comma, then 2-3 digits, a comma and then the last 3 digits. Without the look ahead it would incorrectly match a non-number such as 234,23,412.
If you're going to be picky about commas, like I am being, then you're not going to be able to integrate comma and non-comma cases such as with 1,?9\d{2} => 1914 & 1,914. Having multiple ,? would lead to incorrect matches such as 1000,050, when trying to find a number greater than a million.
You could solve it in two steps: first find a number and then apply a regex to it that checks if it is greater than you number x.
As an example:
here is a regex that matches numbers from 31 to 99999, which you can adjust to your needs:
^(?:[3][1-9]|[4-9][0-9]|(^[1-9][0-9]{2,4}$))$
where
[3][1-9] - matches numbers from 31 to 39
[4-9][0-9] - matches numbers from 40 to 99
^[1-9][0-9]{2,4} - matches numbers from 100 to 9999
The last bit ^[1-9][0-9]{2,4} can be changed to ^[1-9][0-9]{2,} to match any number greater than 100