I have this rule
var reg = new RegExp('[a-z]{3}');
Which means it is allowed to use characters between a-z and at least 3 occurrences.
So, I am wondering if there is a way to match this rule with non sequential characters.
In other words,
"abc" => valid
"aaa" => not valid
Thank you!
Here is a working regex for exactly 3 (or N) characters, if the number is not fixed it gets more complicated:
^([a-z])(?!\1{2})[a-z]{2}$
1 2 3 4 5 6 7 8
Explanation:
^ matches the beginning of the string
([a-z]) match one of the accepted characters and save it (group 1)
(?!...) negative lookahead, what is in those brackets is not accepted
\1 reference to the first group (first character here)
{2} repeated exactly twice
[a-z] the accepted characters
{2} repeated exactly twice
$ matches the end of the string
Link here (I added the gm modifiers, so that several expressions can be tested.)
Try to use the excluding lookahead (?![a-z]{3}), it will not match 3 equal characters in sequence.
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
The objective is to match strings that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits. I thought my regex was enough to do that but is not matching "bana12".
This regex does the job:
var pwRegex = /^\D(?=\w{5})(?=\w*\d{2})/;
Is not this regex more restrictive than mine? Why do I have to specify that the two or more digits are preceded by zero or more characters?
It is less restrictive than yours.
After the \D, there are 2 lookaheads. For your regex, they are
(?=\w{5})(?=\d{2})
This means that the thing after the non-digit must satisfy both of them. That is,
there must be 5 word characters immediately after the non-digit, and
there must be 2 digits immediately after the non-digit.
There is ana12 immediately after the non digit in the string. an is not 2 digits, so your regex does not match.
The working regex however has these two lookaheads:
(?=\w{5})(?=\w*\d{2})
It asserts that there must be these two things immediately after the \D:
5 word characters, and
a bunch of word characters, followed by two digits
ana12 fits both of those descriptions.
Try this Regex101 Demo. Look at step 6 in the regex debugger. That is when it tries to match the second lookahead.
You were on the right track to maybe use lookaheads, and also with the correct start of your pattern, but it is missing a few things. Consider this version:
^\D(?=.*\d{2})\w{4,}$
Here is an explanation of the pattern:
^ from the start of the string
\D match any non digit character
(?=.*\d{2}) then lookahead and assert that two consecutive digits occur
\w{4,} finally match four or more word characters (total of 5 or more characters)
$ end of the string
The major piece missing from your current attempt is that it only matches one non digit character in the beginning. You need to provide a pattern which can match 5 or more characters.
I'm quite new to regex, and not sure what I'm doing wrong exactly.
I'm looking for a regex that match the following number format:
Matching requirements:
Must start with either 0 or 3
Must be between 7 to 11 digits
Must not allow ascending digits. e.g. 0123456789, 01234567
Must not allow repeated digits. e.g. 011111111, 3333333333, 0000000000
This is what I came up with:
^(?=(^[0,3]{1}))(?!.*(\d)\1{3,})(?!^(?:0(?=1|$))?(?:1(?=2|$))?(?:2(?=3|$))?(?:3(?=4|$))?(?:4(?=5|$))?(?:5(?=6|$))?(?:6(?=7|$))?(?:7(?=8|$))?(?:8(?=9|$))?9?$).{7,11}$
The above regex fails the No. (4) condition. Not sure why though.
Any help would be appreciated.
Thanks
A few notes about the pattern that you tried
You can omit the {1} and the comma in [0,3]
In the lookahead (?!.*(\d)\1{3,}) the (\d) is the second capturing group because this (?=(^[0,3]{1})) contains the first capturing group so it should be \2 instead of \1
In the lookahead, you can omit the comma in {3,}
In the match itself you use .{7,11} where the dot would match any character except a newline. You could use \d instead to match only digits
You pattern might look like
^(?=(^[03]))(?!.*(\d)\2{3})(?!^(?:0(?=1|$))?(?:1(?=2|$))?(?:2(?=3|$))?(?:3(?=4|$))?(?:4(?=5|$))?(?:5(?=6|$))?(?:6(?=7|$))?(?:7(?=8|$))?(?:8(?=9|$))?9?$)\d{7,11}$
Regex demo
Or leaving out the first lookahead and move that to the match, changing the quantifier to \d{6,10} and repeating capture group \1 instead of \2
^(?!.*(\d)\1{3})(?!(?:0(?=1|$))?(?:1(?=2|$))?(?:2(?=3|$))?(?:3(?=4|$))?(?:4(?=5|$))?(?:5(?=6|$))?(?:6(?=7|$))?(?:7(?=8|$))?(?:8(?=9|$))?9?$)[03]\d{6,10}$
Regex demo
Edit
Based on the comments, the string not having 4 ascending digits:
^(?!.*(\d)\1{3})[03](?!\d*(?:0123|1234|2345|3456|4567|5678|6789))\d{6,10}$
Regex demo
A solution for a JS flavor of PCRE would be
/^[03](?!123456(7(8(9|$)|$)|$))(?!(?<d>.)\k<d>+$)[0-9]{6,10}$/
Explanations
^[03] starts at the beginning of the string, then reads either 0 or 3
(?!123456(7(8(9|$)|$)|$)) makes sure that, after this first char, there is no sequence (if a sequence can be read, then the negative lookahead fails
(?!(?<d>.)\k<d>+$) is another negative lookahead : it ensures that the first char read (flagged d) is not repeated again and again until end of string
[0-9]{6,10}$/ finally reads 6 to 10 digits (first one already read)
A few tests:
"0123456789: No match"
"01234567: No match"
"01234568: No match"
"011111111: No match"
"33333333: No match"
"333333233 is valid"
"042157891023 is valid"
"019856: No match"
"0123451245 is valid"
I'm trying to write a RegExp to match only 8 digits, with one optional comma maybe hidden in-between the digits.
All of these should match:
12345678
12,45678
123456,8
Right now I have:
^[0-9,]{8}
but of course that erroneously matches 012,,,67
Example:
https://regex101.com/r/dX9aS9/1
I know optionals exist but don't understand how to keep the 8 digit length applying to the comma while also keeping the comma limited to 1.
Any tips would be appreciated, thanks!
To match 8 char string that can only contain digits and an optional comma in-between, you may use
^(?=.{8}$)\d+,?\d+$
See the regex demo
The lookahead will require the string to contain 8 chars. ,? will make matching a comma optional, and the + after \d will require at least 1 digit before and after an optional comma.
If you need to match a string that has 8 digits and an optional comma, you can use
^(?:(?=.{9}$)\d+,\d+|\d{8})$
See the regex demo
Actually, the string will have 9 characters in the string (if it has a comma), or just 8 - if there are only digits.
Explanation:
^ - start of string
(?:(?=.{9}$)\d+,\d+|\d{8}) - 2 alternatives:
(?=.{9}$)\d+,\d+ - 1+ digits followed with 1 comma followed with 1+ digits, and the whole string matched should be 9 char long (8 digits and 1 comma)
| - or
\d{8} - 8 digits
$ - end of string
See the Java code demo (note that with String#matches(), the ^ and $ anchors at the start and end of the pattern are redundant and can be omitted since the pattern is anchored by default when used with this method):
List<String> strs = Arrays.asList("0123,,678", "0123456", // bad
"01234,567", "01234567" // good
);
for (String str : strs)
System.out.println(str.matches("(?:(?=.{9}$)\\d+,\\d+|\\d{8})"));
NOTE FOR LEADING/TRAILING COMMAS:
You just need to replace + (match 1 or more occurrences) quantifiers to * (match 0 or more occurrences) in the first alternative branch to allow leading/trailing commas:
^(?:(?=.{9}$)\d*,\d*|\d{8})$
See this regex demo
You can use following regex if you want to let trailing comma:
^((\d,?){8})$
Demo
Otherwise use following one:
^((\d,?){8})(?<!,)$
Demo
(?<!,) is a negative-lookbehind.
/^(?!\d{0,6},\d{0,6},\d{0,6})(?=\d[\d,]{6}\d).{8}$/
I guess this cooperation of positive and negative look-ahead does just what's asked. If you remove the start and end delimiters and set the g flag then it will try to match the pattern along decimal strings longer than 8 characters as well.
Please try http://regexr.com/3d63m
Explanation: The negative look ahead (?!\d{0,6},\d{0,6},\d{0,6}) tries not to find any commas side by side if they have 6 or less decimal characters in between while the positive look ahead (?=\d[\d,]{6}\d) tries to find 6 decimal or comma characters in between two decimal characters. And the last .{8} selects 8 characters.
This is my current regex check:
const validPassword = (password) => password.match(/^(?=.*\d)(?=.\S)(?=.*[a-zA-Z]).{6,}$/);
I have a check for at least 1 letter and 1 number and at least 6 characters long. However I also want to make sure that there are no spaces anywhere in the string.
So far I'm able to enter in 6 character strings with spaces included :(
Found this answer here, but for some reason in my code it's passing.
What is the regular expression for matching that contains no white space in between text?
It seems you need
/^(?=.*\d)(?=.*[a-zA-Z])\S{6,}$/
Details
^ - start of string
(?=.*\d) - 1 digit (at least)
(?=.*[a-zA-Z]) - at least 1 letter
\S{6,} - 6 or more non-whitespace chars
$ - end of string anchor
With a principle of contrast in mind, you may revamp the pattern into
/^(?=\D*\d)(?=[^a-zA-Z]*[a-zA-Z])\S{6,}$/
Hi all I am making a password regular expression in javascript test() method, It will take the following inputs
solution
/^(?=.*\d)^(?=.*[!#$%'*+\-/=?^_{}|~])(?=.*[A-Z])(?=.*[a-z])\S{8,15}$/gm
May contains any letter except space
At least 8 characters long but not more the 15 character
Take at least one uppercase and one lowercase letter
Take at least one numeric and one special character
But I am not able to perform below task with (period, dot, fullStop)
(dot, period, full stop) provided that it is not the first or last character, and provided also that it does not appear two or more times consecutively.
Can anyone one help me to sort out this problem, Thanks in advance
You may move the \S{8,15} part with the $ anchor to the positive lookahead and place it as the first condition (to fail the whole string if it has spaces, or the length is less than 8 or more than 15) and replace that pattern with [^.]+(?:\.[^.]+)* consuming subpattern.
/^(?=\S{8,15}$)(?=.*\d)(?=.*[!#$%'*+\/=?^_{}|~-])(?=.*[A-Z])(?=.*[a-z])[^.]+(?:\.[^.]+)*$/
See the regex demo
Details:
^ - start of string
(?=\S{8,15}$) - the first condition that requires the string to have no whitespaces and be of 8 to 15 chars in length
(?=.*\d) - there must be a digit after any 0+ chars
(?=.*[!#$%'*+\/=?^_{}|~-]) - there must be one symbol from the defined set after any 0+ chars
(?=.*[A-Z]) - an uppercase ASCII letter is required
(?=.*[a-z]) - a lowercase ASCII letter is required
[^.]+(?:\.[^.]+)* - 1+ chars other than ., followed with 0 or more sequences of a . followed with 1 or more chars other than a dot (note that we do not have to add \s into these 2 negated character classes as the first lookahead already prevalidated the whole string, together with its length)
$ - end of string.