The sample i wanna past through the regex is with follow requirement
list of number seperate by comma
at least input 10 number in list
only accept numbers but could allow empty space in both front and back
Here is my sample regex code:
^(?:(\-|\+|)\d+(?:\.\d*)?|(\-|\+|)\.\d+)+(?:,(?:(\-|\+|)\d+(?:\.\d*)?|(\-|\+|)\.\d+)){9,}$
This regex test code could pass list number seperate by comma,
however when i add empty space in front and on the back it will not work
The following testing Code is failed
' 2,2.5,.5, .678 ,39,1.4.4.8,2.4,2.5,2.6,2.7'
You might use
^\s*[+-]?\d*\.?\d+(?:\.\d+)*(?:\s*,\s*[+-]?\d*\.?\d+(?:\.\d+)*){9,}\s*$
The pattern matches:
^ Start of string
\s*[+-]?\d*\.?\d+ Match an optional plus or minus sign, optional digits and optional dot followed by 1 or more digits
(?:\.\d+)* As there are digits with multiple dot parts, you can optionally repeat that
(?:\s*,\s*[+-]?\d*\.?\d+(?:\.\d+)*){9,} That first part of the pattern matches 1 time. As you want to match at least 10 times you can repeat the first pattern 9 or more times, starting with a comma between optional whitespace chars
\s*$ Optional trailing whitespace chars and assert end of string
Regex demo
Note that \s can also match a newline.
If you don't want that, you could use for example a mere space or [ \t]
Related
Currently, on the project, they have a pattern [^0-9,] and it replaces (String.prototype.replace) everything that we don't need. Except it's not so great. We can add a comma to the start and to the end of the string.
What do I need and I can't do it no matter how hard I try)
first should be a number in the range 0-9
after the first number should be a comma (one comma) or a number or numbers (0-9)
at the end of the line shouldn't be a comma
Correct example,
1,2,3
Incorrect,
,,,,1,,2dgd,d,
1,2,3,
,1,2,,,3
UPD: The method String.prototype.replace() is used on the project.
I'll be grateful if you help me!
If the comma's are optional, maybe you need:
^\d+(?:,\d+)*$
See the online demo
^ - Start string ancor
\d+ - One or more digits.
(?:- Open non-capture group.
,\d+ - A comma followed but one or more digits.
)* - Close non-capture group and match zero or more times.
$ - End string ancor.
Edit:
If you actually want to clean a string, maybe you could use:
(\d+).*?(,(?=.*\d))|\D
See the online demo. Just make sure to replace by $1$2.
(\d+) - 1st Capture group with one or more digits.
.*? - Lazy match anything opto:
(,(?=.*\d)) - 2nd Capture group to match literal comma with a nested positive lookahead to ensure there is still a digit ahead.
| - Or:
\D - Anything other than digit.
const regexp = new RegExp(/(\d+).*?(,(?=.*\d))|\D/g);
const value = '1,2,3'.replace(regexp, '$1$2');
console.log(value)
This should work:
^[0-9]+(?:,[0-9]+)*$
Begins with a digit or a set of digits, followed by zero or more occurrences of a comma followed by one or more digits. Don't miss the start and end line anchors.
Demo
I have the following filtered:
2 digits (?=..*\d)
2 uppercase characters (?=..*[a-z])
2 lowercase characters (?=..*[A-Z])
10 to 63 characters .{10,63}$
Which translates to:
(?=.{2,}\d)(?=..*[a-z])(?=..*[A-Z]).{10,63}
Then I want to exclude a word starting with the letter u, and ending with three to six digits:
([uU][0-9]{3,6})
However, how can I merge these two patterns to do the following:
It should not allow the following because it respectively:
# does not have the required combination of characters
aaaaaaaaaaaaaaa
# is too long
asadsfdfs12BDFsdfsdfdsfsdfsdfdsfdsfdfsdfsdfsdfsdsfdfsdfsdfssdfdfsdfssdfdfsdfssdfdfsdfsdfsdfsdfsfdsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfs
# contains the pattern that shouldn't be allowed
U0000ABcd567890
ABcd56U00007890
D4gF3U432234
D4gF3u432234
ABcd567890U123456
should allow the following:
# it has the required combination of characters
ABcd5678990
ABcd567890
# does contain a part of the disallowed pattern (`([uU][0-9]{3,6})`), but does not fit that pattern entirely
ABcd567890U12
ABcd5U12abcdf
s3dU00sDdfgdg
ABcd56U007890
Created and example here: https://regex101.com/r/4b2Hu9/3
In your pattern you make use of a lookahead (?=..*\d) which has a different meaning than you assume.
It means if what is directly on the right is 2 or more times any char except a newline followed by a single digit and the same for the upper and lowercase variants.
You could update your pattern to:
^(?!.*[uU]\d{3,6})(?=(?:\D*\d){2})(?=(?:[^a-z]*[a-z]){2})(?=(?:[^A-Z]*[A-Z]){2}).{10,63}$
In parts
^ Start of string
(?!.*[uU]\d{3,6}) Negative lookahead, assert not u or U followed by 3-6 digits
(?=(?:\D*\d){2}) Assert 2 digits
(?=(?:[^a-z]*[a-z]){2}) Assert 2 lowercase chars
(?=(?:[^A-Z]*[A-Z]){2}) Assert 2 uppercase chars
.{10,63} Match any char except a newline 10-63 times
$ End of string
Regex demo
First, the way to ensure that the string contains, for example, two digits would be to use a positive lookahead:
(?=.*\d.*\d)
You can generalize this to your other filters.
To make sure the string contains 10 - 63 characters:
.{10,63}
You say you do not want the string to begin with u or U followed by 3 to 6 digits (presumbaly 7 digits is okay), use a negative lookahead:
(?![uU]\d{3,6}\D)
The \D is required to make sure that if there is a 7th digit, then the string will be accepted.
Putting it all together:
r'^(?=.*\d.*\d)(?=.*[a-z].*[a-z])(?=.*[A-Z].*[A-Z])(?![uU]\d{3,6}\D).{10,63}$'
This question already has answers here:
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
(42 answers)
Closed 3 years ago.
I'm trying to create a regex that allows the 4 main character types (lowercase, uppercase, alphanumeric, and special chars) with a minimum length of 8 and no more than 2 identical characters in a row.
I've tried searching for a potential solution and piecing together different regexes but no such luck! I was able to find this one on Owasp.org
^(?:(?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))(?!.*(.)\1{2,})[A-Za-z0-9!~<>,;:_=?*+#."&§%°()\|\[\]\-\$\^\#\/]{8,32}$
but it uses at least 3 out of the 4 different characters when I need all 4. I tried modifying it to require all 4 but I wasn't getting anywhere. If someone could please help me out I would greatly appreciate it!
Can you try the following?
var strongRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!##\$%\^&\*])(?=.{8,})");
Explanations
RegEx Description
(?=.*[a-z]) The string must contain at least 1 lowercase alphabetical character
(?=.*[A-Z]) The string must contain at least 1 uppercase alphabetical character
(?=.*[0-9]) The string must contain at least 1 numeric character
(?=.[!##\$%\^&]) The string must contain at least one special character, but we are escaping reserved RegEx characters to avoid conflict
(?=.{8,}) The string must be eight characters or longer
or try with
(?=.{8,100}$)(([a-z0-9])(?!\2))+$ The regex checks for lookahead and rejects if 2 chars are together
var strongerRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!##\$%\^&\*])(?=.{8,100}$)(([a-z0-9])(?!\2))+$");
reference
I think this might work from you (note: the approach was inspired by the solution to this SO question).
/^(?:([a-z0-9!~<>,;:_=?*+#."&§%°()|[\]$^#/-])(?!\1)){8,32}$/i
The regex basically breaks down like this:
// start the pattern at the beginning of the string
/^
// create a "non-capturing group" to run the check in groups of two
// characters
(?:
// start the capture the first character in the pair
(
// Make sure that it is *ONLY* one of the following:
// - a letter
// - a number
// - one of the following special characters:
// !~<>,;:_=?*+#."&§%°()|[\]$^#/-
[a-z0-9!~<>,;:_=?*+#."&§%°()|[\]$^#/-]
// end the capture the first character in the pair
)
// start a negative lookahead to be sure that the next character
// does not match whatever was captured by the first capture
// group
(?!\1)
// end the negative lookahead
)
// make sure that there are between 8 and 32 valid characters in the value
{8,32}
// end the pattern at the end of the string and make it case-insensitive
// with the "i" flag
$/i
You could use negative lookaheads based on contrast using a negated character class to match 0+ times not any of the listed, then match what is listed.
To match no more than 2 identical characters in a row, you could also use a negative lookahead with a capturing group and a backreference \1 to make sure there are not 3 of the same characters in a row.
^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=[^0-9]*[0-9])(?=[^!~<>,;:_=?*+#."&§%°()|\[\]$^#\/-]*[!~<>,;:_=?*+#."&§%°()|\[\]$^#\/-])(?![a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^#\/-]*([a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^#\/-])\1\1)[a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^#\/-]{8,}$
^ Start of string
(?=[^a-z]*[a-z]) Assert a-z
(?=[^A-Z]*[A-Z]) Assert A-Z
(?=[^0-9]*[0-9]) Assert 0-9
(?= Assert a char that you would consider special
[^!~<>,;:_=?*+#."&§%°()|\[\]$^#\/-]*
[!~<>,;:_=?*+#."&§%°()|\[\]$^#\/-]
)
(?! Assert not 3 times an identical char from the character class in a row
[a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^#\/-]*
([a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^#\/-])\1\1
)
[a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^#\/-]{8,} Match any of the listed 8 or more times
$ End of string
Regex demo
Need help in creating a JavaScript RegEx for matching a string having length between 1 to 5 characters and should not start and end with space. But it can have spaces in between.
I can come up with following
/(^\S\w*){1,5}/
But it is allowing string of length greater than 5 characters and it is also allowing string with trailing space
Assuming m m (three spaces between the m’s) is valid and you you want to allow any non-whitespace characters (\S), not just word characters (\w), you can restate your requirements as:
One non-whitespace character, optionally followed by:
Zero to three characters of any kind
One non-whitespace character
(The key is to group those last two.)
That gives us:
^\S(?:.{0,3}\S)?$
Try it out. (Thanks to RobC for the original examples, which I expanded a bit.)
If you want to match only word characters, you can instead say:
^\w(?:[\s\w]{0,3}\w)?$
Try it out.
As the subject indicates, I am in need of a JavaScript Regular expression X characters long, that accepts alphanumeric characters, but not the underscore character, and also accepts periods, but not at beginning or end. Periods cannot be consecutive either.
I have been able to almost get to where I want to be searching and reading other people's questions and the answers here on Stack Overflow (such as here).
However, in my case, I need a string that has to be exactly X characters long (say 6), and can contain letters and numbers (case insensitive) and may also include periods.
Said periods cannot be consecutive and also, cannot start, or end the string.
Jd.1.4 is valid, but Jdf1.4f is not (7 characters).
/^(?:[a-z\d]+(?:\.(?!$))?)+$/i
is what I have been able to construct using examples from others, but I cannot get it to only accept strings that match the set length.
/^((?:[a-z\d]+(?:\.(?!$))?)+){6}$/i
works in that it now accepts nothing less than 6 characters, but it also happily accepts anything longer as well...
I am obviously missing something, but I do not know what it is.
Can anyone help?
This should work:
/^(?!.*?\.\.)[a-z\d][a-z\d.]{4}[a-z\d]$/i
Explanation:
^ // matches the beginning of the string
(?!.*?\.\.) // negative lookahead, only matches if there are no
// consecutive periods (.)
[a-z\d] // matches a-z and any digit
[a-z\d.]{4} // matches 4 consecutive characters or digits or periods
[a-z\d] // matches a-z and any digit
$ // matches the end of the string
Another way to do that:
/(?=.{6}$)^[a-z\d]+(?:\.[a-z\d]+)*$/i
explanation:
(?=.{6}$) this lookahead impose the number of characters before
the end of the string
^[a-z\d]+ 1 or more alphanumeric characters at the beginning
of the string
(?:\.[a-z\d]+)* 0 or more groups containing a dot followed by 1 or
more alphanumerics
$ end of the string