Need a regular expression that contains one capital letter, 8-25 characters long, and contains one special character (excluding # and &). Here is what I have:
/^(?=.*[A-Z])(?=.*[^0-9a-zA-Z]).{8,25}$/
What do I need to add so that the regex does not accept the string if it contains a # or &?
Simply:
/^(?=.*[A-Z])(?=.*[^0-9a-zA-Z])[^#&]{8,25}$/
Explanation:
(?=.*[A-Z]) means that the match succeeds only if there is 0-n characters, followed by a capital letter.
(?=.*[^0-9a-zA-Z]) means that the match succeeds only if there is 0-n characters, followed by a character that is not 0-9, a-z or A-Z.
[^#&]{8,25} means that the main match must consist of 8-25 characters, none of which can be # or &
. will match any character apart from newline; replace it with [^#&]:
/^(?=.*[A-Z])(?=.*[^0-9a-zA-Z])[^#&]{8,25}$/
Related
I am trying to create a regex pattern for name validation.
Application name must have the following:
Lowercase alphanumeric characters can be specified
Name must start with an alphabetic character and can end with alphanumeric character
Hyphen '-' is allowed but not as the first or last character
e.g abc123, abc, abcd-1232
This is what I got [^\[a-z\]+(\[a-z0-9\-\])*\[a-z0-9\]$][1] it doesn't work perfectly. The validation fails if you enter a single character in the field. How can I improve this pattern? Thank you in advance.
You may use the following pattern:
^[a-z](?:[a-z0-9-]*[a-z0-9])?$
Explanation:
^[a-z] starts with lowercase alpha
(?: turn off capture group
[a-z0-9-]* zero or more alphanumeric OR dash
[a-z0-9] mandatory end in alphanumeric only, if length > 1
)? make this group optional
$ end of input
You are using a negated character class [^ that matches 1 character, not being any of the specified characters in the character class.
That is followed by another character class [1] which can only match 1, so the pattern matches 2 characters, like for example #1
In your examples you seem to have only a single hyphen, so if there can not be consecutive hyphens --
^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$
Explanation
^ Start of string
[a-z] Match a single char a-z
[a-z0-9]* Optionally repeat any of a-z or 0-9
(?:-[a-z0-9]+)* Optionally repeat matching - followed by at least 1 or a-z or 0-9 (So there can not be a hyphen at the end)
$ End of string
Regex demo
I've got this RegEx which is used to validate what the user enters
It must be a value 8 - 16 characters long and can contain ONE of the certain special characters.
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[:;#~]).{8,16}$"
I'm not trying to show an alert if the user enters something that doesn't match the above. So a-z, A-Z, 0-9 and :;#~ are allowed but anything else shows an alert.
So Abcd1234# is OK, but if they enter Abcd1234!$ if will show the alert as ! & $ are not in the match.
I've tried adding ^ to the start of character match to try and negate them, but that didn't work.
What's the best way to do this?
It seems you only need to allow the characters mentioned in the lookaheads, create a character class with them and replace the last . with it:
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[:;#~])[\da-zA-Z:;#~]{8,16}$/
^^^^^^^^^^^^^^
See the regex demo
The [\da-zA-Z:;#~]{8,16} pattern will match 8 to 16 chars that are either digits, ASCII letters, or :, ;, # or ~ symbols.
Details:
^ - start of string
(?=.*\d) - there must be a digit after any 0+ chars other than line break chars (a (?=\D*\d) will be more efficient as it is based on the contrast principle)
(?=.*[a-z]) - - there must be an ASCII lowercase letter after any 0+ chars other than line break chars (a (?=[^a-z]*[a-z]) will be more efficient)
(?=.*[A-Z]) - there must be an ASCII uppercase letter after any 0+ chars other than line break chars (a (?=[^A-Z]*[A-Z]) will be more efficient)
(?=.*[:;#~]) - there must be a :, ;, # or ~ after any 0+ chars other than line break chars (you may also use (?=[^:;#~]*[:;#~]))
[\da-zA-Z:;#~]{8,16} - 8 to 16 chars defined in the character class
$ - end of string.
HI I am new to regular expression, I tried creation regular expression based on below conditions:
Maximum 9 characters are allowed
First character must be upper case
Ending character must be 0-9
Must contain following special character ($,%,#)
/^[A-Z][a-z0-9A-Z$#%]{3,9}(?=.*[#$%]).\d+$/
What is wrong in my regular expression?
^[A-Z](?=.*[#$%])[a-z0-9A-Z$#%]{1,7}\d$
You need to take the lookahead at the start.\d+ should be \d.{3,9} should be {1,7}
Breaking the regex down
/^[A-Z][a-z0-9A-Z$#%]{3,9}(?=.*[#$%]).\d+$/
^ # Match the start of a string
[A-Z] # First character must be a capital letter
[a-z0-9A-Z$#%]{3,9} # The next 3-9 characters must be alphanumeric or one of $, # and %.
(?=.*[#$%]) # Look-ahead, requiring that some character be one of $, # and % (note that this is strictly after the 3-9 character check)
. # Match any character
\d+ # Match one or more numeric digits
$ # Match the end of the string
Therefore a string like "Aaaa$^55555555555555555" would be matched.
You need to change your look-ahead, probably moving it to before the 3-9 character check. You'll also want to make the length of that smaller, since you're explicitly allowing a capital letter as the first character and digit as the last character, so you'll probably want to match 1-7 characters instead of 3-9.
/^[^ ]([\w- \.\\\/&#]+)[^ ]$/,
I have the above regex. I want to make sure it accepts all special characters but i don't want to specify the entire special character listsuch as [\w- \.\\\/&#!##$&]. How can we make sure the above regex accepts all special characters
[^\w\s] matches any non-alphanumeric and non-whitespace character.
\S matches any non-whitespace character.
. matches any character except newlines.
[\S\s] matches any character in a JavaScript regex.
Since you've got \w and a space in there already, you must want all of the ASCII characters except control characters. That would be:
[ -~]
...or any character whose code point is in the range U+0020 (space) to U+007E (tilde). But it looks like you want to make sure the first and last characters are not whitespace. In fact, looking at your previous question, I'll assume you want only letters or digits in those positions. This would work:
/^[A-Za-z0-9][ -~]*[A-Za-z0-9]$/
...but that requires the string to be at least two characters long. To allow for a single-character string, change it to this:
/^[A-Za-z0-9](?:[ -~]*[A-Za-z0-9])?$/
In other words, if there's only one character, it must be a letter or digit. If there are two or more characters, the first and last must letters or digits, while the rest can be any printing character--i.e., a letter, a digit, a "special" (punctuation) character, or a space.
Note that this only matches ASCII characters, not accented Latin letters like  or ë, or symbols from other alphabets or writing systems.
. matches any character except for newline.
1) ^[^\s].{1,20}$
2) ^[-/##&$*\w\s]+$
3) ^([\w]{3})$
Are there any links for more information?
^[^\s].{1,20}$
Matches any non-white-space character followed by between 1 and 20 characters. [^\s] could be replaced with \S.
^[-/##&$*\w\s]+$
Matches 1 or more occurances of any of these characters: -/##&$*, plus any word character (A-Ba-b0-9_) plus any white-space character.
^([\w]{3})$
Matches three word characters (A-Ba-b0-9_). This regular expression forms a group (with (...)), which is quite pointless because the group will always equal the aggregate match. Note that the [...] is redundant -- might as well just use \w without wrapping it in a character class.
More info: "Regular Expression Basic Syntax Reference"
1) match everything without space what have 1 to 20 chars.
2) match all this signs -/##&$* plus words and spaces, at last one char must be
3) match three words
here is excelent source of regex
http://www.regular-expressions.info/
Matches any string that starts with a non-whitespace character that's followed by at least one and up to 20 other characters before the end of the string.
Matches any string that contains one or more "word" characters (letters etc), whitespace characters, or any of "-/##&$*"
Matches a string with exactly 3 "word" characters