I would like to create a regex, that allowes the following patterns:
1234
1234567
123456789
12345678900-
12345678900-123456
It should be possible to only insert numbers and only one hyphen is allowed.
I tried with the following regex:
^[0-9]{1,11}(?(?<=\d{11})[-]?|)[0-9]{6}
It should not be possible to have 11 characters without the hyphen at the end(12345678900 is wrong).
Unfortunatly it didnt work as I intended.
You can match 1-10 digit and optionally match 1 digit followed by - and 6 digits.
^\d{1,10}(?:\d?-(?:\d{6})?)?$
^ Start of string
\d{1,10} Match 1-10 digits
(?: Non capture group
\d?- Match a single optional digit and -
(?:\d{6})? Match optional 6 digits
)? Close non capture group and make it optional
$ End of string
Regex demo
Another variation could be matching 1-10 digits or match 11 digits with a hyphen and optionally 6 digits if the hyphen should only possible after 11 digits.
^(?:\d{1,10}|\d{11}-(?:\d{6})?)$
Regex demo
You can use
^[0-9]{1,11}(?:(?<=[0-9]{11})-(?:[0-9]{6})?)?$
^\d{1,11}(?:(?<=\d{11})-(?:\d{6})?)?$
See the regex demo. Using \d is possible in case it matches ASCII digits in your regex flavor or you do not care if \d matches all possible Unicode digit chars or not.
Details:
^ - start of string
[0-9]{1,11} - one to eleven digits
(?:(?<=[0-9]{11})-(?:[0-9]{6})?)? - an optional occurrence of
(?<=[0-9]{11}) - immediately to the left there must be 11 digits
- - a hyphen
(?:[0-9]{6})? - an optional occurrence of six digits
$ - end of string.
Related
Validate the string using the regex which has the - (hypen)
Requirement is : string contains only digits and - (hyphens) and not end with - (hyphen) and all other digits not be the same.
^([0-9-])(?!\1+$)[0-9-]{5}$
The above one allow only digits and hyphen but its not restricted end with hyphen and check all other digits are same.
ex:
1111-1 Not allowed because all are same digits
1111-2 Allowed
11112- Not allowed as its end with - Hypen
-12345 Not allowed as its start with - hypen
You might write the pattern as
^(\d)(?!(?:\1|-)+$)(?!\d*-\d*-)[\d-]{4}\d$
Explanation
^ Start of string
(\d) Capture a single digit in group 1
(?! Negative lookahead
(?:\1|-)+$ Check that to the right there is not only the group 1 value or hyphens
(?!\d*-\d*-) Assert not 2 hyphens
) Close lookahead
[\d-]{4} Match 4 digits or hyphens
\d Match a digit
$ End of string
Regex demo
If there should be at least 1 hyphen:
^(\d)(?!(?:\1|-)+$)(?=\d*-)[\d-]{4}\d$
Regex demo
My 2 cents to allow [01] hyphens:
^(?=.{6}$)(\d)(?=.*(?!\1)\d)\d+(?:-\d+)?$
See an online demo
I am looking for a regex that matches:
Length is between 1 and 10.
The 8th character can be alphanumeric [0-9a-zA-Z] and the rest have to be digits [0-9]
Valid
1
123
1234567890
1234567a
Invalid
1a
123456789a
12345678901
I have tried: [0-9]{1,7}[0-9a-zA-Z][0-9]{0,1} but that is failing miserably
You can match either 1-7 digits, or match 7 digits, the 8th one being [0-9a-zA-Z] and optional 2 digits.
^(?:\d{7}[\da-zA-Z]\d{0,2}|\d{1,7})$
^ Start of string
(?: Non capture group
\d{7}[\da-zA-Z]\d{0,2} Match 7 digits and one of 0-9a-zA-Z] and 2 optional digits
| Or
\d{1,7} Match 1-7 digits
) Close non capture group
$ End of string
Regex demo
You could try:
^(?!.{11})\d(?:\d{6}[^\W_])?\d*$
See an online demo
^ - Start-line anchor;
(?!.{11}) - Negative lookahead to prevent 11 chars;
\d - A single digit;
(?:\d{6}[^\W_])? - Optional non-capture group to match 6 more digits and any character in class [A-Za-z0-9];
\d* - 0+ (Greedy) digits;
$ - End-line anchor.
Well you could simplify The fourth birds solution by using \w
Like so:
^(?:\d{7}\w\d{0,2}|\d{1,7})$
But that’s basically the same.
I have an application that needs to handle validation for phone numbers. Phone numbers are required to have 13 characters (sum of numbers and dashes). There must be at least 1 dash and a maximum of 3 dashes. The starting character must be a digit. How can I create a regex for this validation? Here is my regex string. /^(?:[0-9]-*){13}$/ It doesn't work exactly what I expected
So 13 characters in total with a maximum of 3 dashes and a minimum of 1 means 10 digits right? Therefor your characters are ranging 11-13?
If so, try:
^(?=(?:\d-?){10}$)\d+(?:-\d+){1,3}
See an online demo
^ - Start line anchor.
(?= - Open a positive lookahead:
(?: - Open a non-capture group:
\d-? - Match a digit and optional hyphen.
){10}$) - Close the non-capture group and match it ten times before the end-string anchor. Then close the lookahead.
\d+ - 1+ Digits.
(?: - Open a 2nd non-capture group:
-\d+ - Match an hyphen and 1+ digits.
){1,3} - Close non-capture group and match it 1-3 times.
You can use
^(?=.{13}$)[0-9]+(?:-[0-9]+){1,3}$
^(?=.{13}$)\d+(?:-\d+){1,3}$
See the regex demo. Details:
^ - start of string
(?=.{13}$) - the string must contain exactly 13 chars
[0-9]+ / \d+ - one or more digits
(?:-[0-9]+){1,3} / (?:-\d+){1,3} - one, two or three repetitions of a hyphen followed with one or more digits
$ - end of string.
See the regex graph:
A JavaScript demo:
const texts = ['123-4567-8901','123-45-67-890','123-456728901','1234567890123','123--67890123','-234567890123','123456789012-','-23456789012-'];
const regex = /^(?=.{13}$)\d+(?:-\d+){1,3}$/;
for (const text of texts) {
console.log(text, "=>", regex.test(text));
}
I assumed the number of digits are not known. I am looking for number of dashes between 1 and 3 between the numbers
texts = ['1-2-3-4-5','123-4567-8901','123-45-67-890','123-456728901','1234567890123','123--67890123','-234567890123','123456789012-','-23456789012-']
for text in texts:
print(text,re.findall(r"^\d+-?\d+-?\d+-?\d+$",text))
output:
1-2-3-4 ['1-2-3-4']
123-4567-8901 ['123-4567-8901']
123-45-67-890 ['123-45-67-890']
123-456728901 ['123-456728901']
1234567890123 ['1234567890123']
123--67890123 []
-234567890123 []
123456789012- []
-23456789012- []
I a looking for a Regex to match a string which should:
start with a digit
'in-between' have a permutation of exactly 7 digits and 2 hyphens, without 2 consecutive hyphens
end with a sequence of digit, hyphen, digit
Match:
01-234-5678-9
01234-56-78-9
0123-4-5678-9
012-345-678-9
01-234567-8-9
01-234-5678-9
0-12345-678-9
0-123-45678-9
0-123-45678-9
01-23456-78-9
0-123456-78-9
0-1234567-8-9
No Match:
01-234-56789-0
01-234-567-8
01--2345678-9
01-2345678--9
0-1-23456789
-01-2345678-9
For now, I could not quite figure out how to match the 2 'in-between' hyphens: ^\d\d{7}\d-\d$
EDIT:
Thanks to the answers I had to this question, I was able to expand it to this other question regarding ISBN-10 and ISBN-13...
You can assert 7 digits and the digit - digit part at the end.
For the match there should be at least a single digit before and after the hyphen to prevent consecutive hyphens.
^\d(?=(?:-?\d){7}-?\d-\d$)\d*-\d+-\d*\d-\d$
^ Start of string
\d Match a single digit
(?= Positive lookahead
(?:-?\d){7} Match 7 digits separated by an optional -
-?\d-\d$ Match an optional - and the \d-\d$ at the end
) Close the lookahead
\d*-\d+-\d*\d-\d Match possible formats where all hyphens are separated by at least a single digit
$ End of string
Regex demo
My two cents:
^(?=.{11}-\d$)(?:\d+-){3}\d
See the online demo
^ - Start string anchor.
(?= - Open positive lookahead:
.{11}-\d$ - Any character other than newline 11 times followed by a hypen, a single digit and the end string anchor.
) - Close positive lookahead.
(?: - Open non-capture group:
\d+- - 1+ digit followed by an hyphen.
){3} - Close non-capture group and match three times.
\d - Match a single digit.
I guess alternatively even ^(?=.{13}$)(?:\d+-){3}\d$ would work.
I am trying to write a regular expresion that checks if a string starts with a number of digits (at least one), and then immediately ends with a single letter or a digit.
So:
29c is fine
29 is fine
2425315651252fsaw fails
24241jl.421c fails
c fails
The regex I have so far is (^\d+)([a-z]{1}|\d) which passes the 29, 20c, but also passes stuff like 29cdsd.
What am I doing wrong?
Your (^\d+)([a-z]{1}|\d) passes 29cdsd because it matches 1 or more digits at the start of the string followed with 1 letter or 1 digit, and allows anything right after.
Use
^[0-9]+[a-z0-9]?$
See regex demo
Details
^ - start of string
[0-9]+ - any 1 or more digits
[a-z0-9]? - 1 or 0 lowercase ASCII letters or digits
$ - end of string.
This should follow your rules exactly.
^\d+[a-z]?$
if "any number of digits" might be zero ^\d*\w$
You could add an anchor $ to assert the end of the line and you can omit the {1} part:
^(\d+)([a-z]|\d)$
In your regex you are matching a minimum of 2 characters .If you don't need the capturing groups, this could also be written as:
^\d+[a-z\d]$
Regex demo
That would match:
^ Assert the start of the string
\d+ Match 1+ digits
[a-z\d] A character class which matches a-z or a digit
$ Assert the end of the string
^ - start of string
\d* - any amount of digits, 0 or more
[a-zA-z] - a lowercase and uppercase ASCII letters.
$ - end of string