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.
Related
Please assist me with creating this regex.
A total/exactly 3 digit alphanumeric
Exactly 1 numeric excluding 0 and 1 2-9
Exactly 2 Alpha excluding letter O and L - o and l
Number can be in any position
Valid codes:
A2M
HH9
3AM
Invalid Codes
10M (too many digits and invalid digits,
22A (two many digits),
MAB (missing digit)
MA2M (too long, not length of 3)
thank you for all the help. Here is the regex I will use, this one removes the letter L and lowercase l:
/([2-9]{1}[A-KMNP-Za-kmnp-z]{2}|[A-KMNP-Za-kmnp-z]{1}[2-9]{1}[A-KMNP-Za-kmnp-z]{1}|[A-KMNP-Za-kmnp-z]{2}[2-9]{1})/g
You may be able to shorten your regex with use of a lookahead:
^(?=\D*\d\D*$)[A-KMNP-Za-kmnp-z2-9]{3}$
RegEx Demo
RegEx Details:
^: Start
(?=\D*\d\D*$): Lookahead to ensure that we have exactly one digit
[A-KMNP-Za-kmnp-z2-9]{3}: Match any letter or digit except [01LlOo] exactly 3 times
$: End
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
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
I am trying to validate a field which can take input as following ways:
should take 1 to 4 alpha charcters.(but should start with alpha)
from 5th position to so on should take numbers.(no where from 5th should accept alphabets)
in between 1-4 characters of alpha it should not allow numbers.
4.even if first 4 characters are entered it should accept.(that 4 characters should be alpha.ex:"asdf")
^[a-zA-Z][0-9]$
i have many things and searched many sites.i could not find it.please help me.
Thank you in advance.
For an answer off the top of my head:
^[a-zA-Z]{1,4}[0-9]+$
will match a string with the following break down:
^ = Start of string
[a-zA-Z] = a through z (case-insensitive)
{1,4} = 1 to 4 times
[0-9]+ = one or more numbers
$ = End of string
Because each situation is different, I would suggest using an online regex tester to test certain strings of characters.
Try this:
^[A-Za-z]{1,4}\d*$
https://regex101.com/r/yH6pR3/1
It will only allow 1-4 alpha characters, and then only digits thereafter. Digits are optional. You can change that by making it \d+ instead.
I am wondering what you mean by digits only from 5th position onward. What if there are three or less alpha at the start of the string?
UPDATE:
^(?:[A-Za-z]{1,4}|[A-Za-z]{4}\d+)$
https://regex101.com/r/qQ8nR2/1
First it attempts to match just 1-4 characters. If that fails, then it attempts to match 4 characters followed by 1 or more digits.
You can also write it this way:
^\p{L}{1,4}\d+$
\p{L}{1,4} matches any letter 1 to 4 times
\d+ matches any digit one or more times
^[a-zA-Z]{1,4}\d*
[a-zA-Z] is for alpha chars
`\d' is short for numbers
{1,4} specify 1 to 4 chars
* specify any number of digits (including none)
I need help with a regex
I need it to match either a 9 or 10 digit value that starts with 50.
I have:
^[ ]*(50)[0-9]{7}[ ]*$
which allows 9 digits.
How can I expand this so that it also allows 10 digits?
Add the range {7,8}
^[ ]*(50)[0-9]{7,8}[ ]*$
FYI this site describes the standard quantifiers that you can use in a regular expression:
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
Try with following regex:
^[ ]*50\d{7,8}[ ]*$
This regex will match what you need:
^\s*50\d{7,8}\s*$
This will match all 9 or 10 digit numbers starting with 50 with an unlimited number of spaces before, or after, them on the line.
If you want to match all 9 or 10 digit numbers starting with 50 regardless of position and number of spaces etc then:
50\d{7,8}
will do exactly what you need.
Here's what you need: (50)\d{7,8}