Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to implement regex validation for passport number.
Length: 5-20 char Alphanumeric
Allowed characters: a-z, A-Z, 0-9 (case insensitive)
Cannot contain only 1 digit or character, repeated
Example: (what I expect to see)
Z1234aZ - false // "Z" is repeated
aVBNa1 - false // "a" is repeated
ZXCVB123 - true
12zv - false // length 4
My regex: /^(.){2,}[A-Za-z0-9]{5,20}$/ but it doesn't work
How can i match that none of symbols doesn't repeat?
I'm thinking maybe you can use:
^(?:([A-Z\d])(?!.*\1)){5,20}$
See an online demo. Using case-insensitive matching it would mean:
^ - Start line anchor.
(?: - Open non-capture group:
([A-Z\d]) - Capture single character from class.
(?!.*\1) - Negative lookahead to prevent matched character to occur again using a backreference to 1st capture group.
){5,20} - Close non-capture group and match 5-20 times.
$ - End line anchor.
Not sure if I read it correctly, but if you require at least two digits and at least to alpha-chars then maybe add two positive lookaheads:
^(?=.*\d.*\d)(?=.*[A-Z].*[A-Z])(?:([A-Z\d])(?!.*\1)){5,20}$
You can try this,
^(?!^0+$)[a-zA-Z0-9]{3,20}$
Or
^[A-PR-WYa-pr-wy][1-9]\d\s?\d{4}[1-9]$
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 days ago.
Improve this question
I have the following regex that I will use in Javascript eventually:
^(0|[1-9][0-9]*)([,;|\t]|(?:x) | {2,})
Given this sample text, I would like to get on the capture group 1 the quantity and on the capture group 2 the separator (either a comma, semicolon, pipe, tab, two or more spaces or a "x" followed by a single space):
2,Apples
3x Trees
2 Oranges
2x A3252893
15553x Moons
15553x Apples Test test
Regex101 example
Now, the question is about me wanting to match the single space as separator only if it is preceded by the "x" character, i.e. basically leaving out the "x" from the group. If I change the regex above by replacing the non-capturing group with the positive lookbehind, it doesn't work:
^(0|[1-9][0-9]*)([,;|\t]|(?<=x) | {2,})
Regex101 updated example
Why doesn't it match? Is that because it is used inside an alternation? And how can I fix that?
EDIT: OK, so positive lookbehind is not the correct solution because I didn't match any x before that. So how can I match the separators that I've listed above on the same numbered capturing group 2 without including the x but only the space (i.e. capture the space only if its preceded by an x while keeping the other variants as they are)?
The problem is that the look behind assertion for an "x" comes at a moment that the digits have been matched, so the previous character will always be a digit, not "x".
You can solve this by reading the "x" first (when it is there), before continuing. So insert x?
^(0|[1-9]\d*)x?([,;|\t]|(?<=x) | {2,})
On regex101
If you don't want to allow the "x" to be there when another delimiter (or two spaces) is used, then use a negative look behind assertion in those cases:
^(0|[1-9]\d*)x?((?<!x)[,;|\t]|(?<=x) |(?<!x) {2,})
On regex101
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want a regular expression which validates my input e.g "S19/999999/090". my input field should accept this kind of input. means the first character should be an uppercase alphabet from A-Z after that two digits from 0-9. after that one backslash. after that 6 digits from 0-9 after that one backslash. after that 3 digits from 0-9.
E.g "S19/999999/090"
I don't understand how to create this kind of regular expression.
Thank you in advance
check the attached fiddle. [https://regex101.com/r/IQsyv2/1/]
and regex will be like -
^[A-Z][\d]{2}/[\d]{6}/[\d]{3}$
here initially to notice I have specified ^ and $ these char are used to define the full length match, mean ^ - says starting and $ says ending.
[A-Z] - mean check uppercase only once.
[\d]{2} - mean [\d] any digit, {2} - 2 times
\/ -- mean match backslash
[\d]{6} - only digit 6 times exactly.
\/ -- mean match backslash
[\d]{3} - only digit 3 times exactly.
Try this
[A-Z]\d{2}\/\d{6}\/\d{3}
[A-Z] - one character A-Z uppercase
\d - digital
\d{n} - n digital
/\ - /
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to create an expression that fit this requirements :
The string must be composed by 3 substring
The first substring accept 0-9a-zA-Z, the minimum length is 1 and there is notte max length
The second substring must be " - "
The last have The first's one same condition
Total maximum string length must be 28 chars
It is possible to accomplish this requirement with Regex?
Following regex should work fine:
/(?=^.{3,28}$)[^\W_]+\-[^\W_]+/
var array = [
"123456790-123456789012345678",
"123456790-1234567890123456789",
"adsd-dsds"
];
var re = /(?=^.{3,28}$)[^\W_]+\-[^\W_]+/;
array.forEach(e => console.log(re.test(e) ? e.match(re)[0]: "match failed"));
Breakdown shamelessly copied from regex101.com:
Positive Lookahead (?=^.{3,28}$)
^ asserts position at start of a line
.{3,28} matches any character (except for line terminators)
{3,28} Quantifier — Matches between 3 and 28 times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of a line
Match a single character not present in the list below [^\W_]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\W matches any non-word character (equal to [^a-zA-Z0-9_])
_ matches the character _ literally (case sensitive)
\- matches the character - literally (case sensitive)
Match a single character not present in the list below [^\W_]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\W matches any non-word character (equal to [^a-zA-Z0-9_])
_ matches the character _ literally (case sensitive)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have two words within sentence.
EX: big BUTTON
here I need to check second word is in uppercase using regex expression.
Square brackets [] are your friend. They allow you to specify characters that will match. To match the first work, you need to check for any letter. This can be done with [a-zA-Z]. This will match any letter between a and z, as well as A and Z. for the second word, you only want to match uppercase, so only use [A-Z]. To get 1 or more matches, put a + after the closing bracket.
Putting this all together, with a space in between the words, you get [a-zA-Z]+ [A-Z]+.
The carat ^ is used to signify the start of the string, and the dollar sign $ is used to signify the end of the string. Your question somewhat vague, so here are a couple scenarios:
Each sentence is only two words with a space in between them: ^[a-zA-Z]+ [A-Z]+$
Each sentence has at least two words and may or may not end in a period: ^[a-zA-Z]+ [A-Z]+( |\.?$)
In the second example the parenthesis with a pipe (|) is used as an OR statement. The period is escaped since it is a special character (matches any single character). The question mark denotes 0 or 1 of the preceding character, which is a period. So ( |\.?$) will match a space or a sentence that ends with or without a period.
Here is a good site that has information on Regexes: http://www.regular-expressions.info/
This regexp looks for any sequence, starting at the beginning of the string (^), of alphanumeric characters (\w)--that's the first word--then a space, followed by a sequence of upper-case letters ([A-Z]+)--the second word--followed by either a space or the end of the string ($).
/^\w+ [A-Z]+( |$)/
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need a regular expression for a string that
starts with Alphabets (no number)
max Length 8
No special characters or space.
string can have number or _ except for starting character.
This would work:
/^[a-z][a-z0-9_]{0,7}$/i
For example,
/^[a-z][a-z0-9_]{0,7}$/i.test('a1234567'); // true
/^[a-z][a-z0-9_]{0,7}$/i.test('01234567'); // false
The \w shorthand is for all letters, numbers and underscores. [A-Za-z] is overkill, the /i flag will get you all letters, case insensitive.
Therefore, a super simple regex for what you need is:
/^[a-z]\w{0,7}$/i
/^[a-z]\w{0,7}$/i.test("a1234567");
> true
/^[a-z]\w{0,7}$/i.test("a12345697");
> false
/^[a-z]\w{0,7}$/i.test("01234567");
> false
Try this out:
/^[A-Za-z]{1}[a-zA-Z0-9_]{0,7}$/
Try this one:
/^[a-zA-Z][0-9a-zA-Z_]{0,7}$/
This requires an alpha start character, and optionally allows up to 7 more characters which are either alphanumeric or underscore.
EDIT: Thanks, Jesse for the correction.
And another version with lookaheads :)
if (subject.match(/^(?=[a-z]\w{0,7}$)/i)) {
// Successful match
}
Explanation :
"^" + // Assert position at the beginning of the string
"(?=" + // Assert that the regex below can be matched, starting at this position (positive lookahead)
"[a-z]" + // Match a single character in the range between “a” and “z”
"\\w" + // Match a single character that is a “word character” (letters, digits, etc.)
"{0,7}" + // Between zero and 7 times, as many times as possible, giving back as needed (greedy)
"$" + // Assert position at the end of the string (or before the line break at the end of the string, if any)
")"