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
Related
Allowable characters:
uppercase A to Z
lowercase a to z
hyphen
apostrophe
single quote
space
full stop
numerals 0 to 9
validations:
Must contain alphabetic characters
Cannot have consecutive non alpha characters except for full stop followed by a space OR apostrophe can be followed by a space
Cannot have non-alphabetic characters at the start (except for apostrophe)
Can end with a full stop
I have a regex with the below validation. Use this as reference
/^(?=[a-zA-Z0-9`'. -]+$)(?!.*[0-9'` -]{2})[a-zA-Z'][^\r\n.]*(?:\.[ a-z][^\r\n.]*)*$/;
Need to add the below validations to the above regex
Can end with a full stop
An apostrophe can be followed by a space.
Examples Valid
'Andy
Andy.
Andy' De'Wall
Andy. DeWa2e
A2dy'
Examples Invalid
2Andy
A2'ndy.
Andy'-Wall
Andy. DeWa23
A24dy'
You could:
assert not 2 consecutive digits . ' or -
assert not a digit or hyphen or space followed by a space
assert at least a char A-Z a-z
start the match with either ' or a char A-Z a-z
For example
^(?!.*[0-9.'-]{2})(?!.*[0-9 -] )(?=[^A-Za-z\n]*[A-Za-z])[A-Za-z'][A-Za-z0-9.' -]*$
Regex demo
I'm trying to create a regular expression that matches strings such as:
N1-112S
So far I have succeeded with the following (although I'm not really sure why it works):
item.match(/^\D.-/)
I'd like to further bolster the results by ensuring that the last character is A-Z as well.
I'd appreciate some help on a good regular expression for matching this pattern. Thanks!
If you plan to match a string that starts with an uppercase ASCII letter, then has a digit, then a hyphen, then 1 or more digits and then an ASCII letter at the end of the string use
/^[A-Z]\d-\d+[A-Z]$/.test(item)
See the regex demo. Also, to test if a regex matches some string or not, I'd recommend RegExp#test.
Pattern details
^ - start of string
[A-Z] - an uppercase ASCII letter
\d - an ASCII digit
- - a hyphen
\d+ - 1+ digits
[A-Z] - an ASCII letter
$ - end of string.
Variations
To match any alphanumeric chars after hyphen till the end of string, you need to change the above pattern a bit:
/^[A-Z]\d-[\dA-Z]*[A-Z]$/
The second \d+ is changed to [\dA-Z]*, any 0 or more ASCII digits or letters.
If there can be any chars after -, use .* or [^] instead of a \d+:
/^[A-Z]\d-.*[A-Z]$/
I have the following javascript regex:
/^[^\s][a-z0-9 ]+[^\s]$/i
I need to allow any alphanumeric character as well as spaces inside the string but not at the beginning nor at the end.
Oddly enough, the above regex will not accept less than 3 characters, e.g. aa will not match but aaa will.
I am not sure why. Can anyone please help ?
You have: [^\s] (requires matching at least one non-whitespace character), [a-z0-9 ]+ (requires matching at least one alphanumeric or space character), and [^\s] again (requires matching at least one non-whitespace character). So, in total, you need at least 3 characters in the string.
Use word boundaries at the beginning and end instead:
/^\b[a-z0-9 ]+\b$/i
https://regex101.com/r/2GhH3N/1
Try the following regex:
^(?! )[a-z0-9 ]*[a-z0-9]$
Details:
^(?! ) - Start of the string and no space after it (so here we exclude the
initial space).
[a-z0-9 ]* - A sequence of letters, digits and spaces, possibly empty
(the content before the last letter(see below).
[a-z0-9]$ - The last letter and the end of string (so here we exclude the
terminal space).
You should re-write the expression as
/^[a-z0-9]+(?:\s+[a-z0-9]+)*$/i
See the regex demo.
NOTE: If only one whitespace is allowed between the alphanumeric chars use
/^[a-z0-9]+(?:\s[a-z0-9]+)*$/i
^^
Details
^ - start of string
[a-z0-9]+ - 1+ letters/digits
(?:\s+[a-z0-9]+)* - 0 or more repetitions of 1+ whitespaces (\s+) and 1+ digit/letters
$ - end of string.
See the regex graph:
New to regex. I am validating a input filed. I need to write regular expression which checks the following:
Only letters and spaces are allowed not numbers and special charactes
The first character should be a valid letter [a-zA-Z] not a space.
Thank you
The following regex accepts only letters or spaces as you wanted.
^(?! )[A-Za-z\s]*$
Quick explanation:
^ Matches beginning of input
(?! ) Zero-width negative look-ahead assertion a.k.a. "not followed by...", so the first letter won't be a space character
[A-Za-z\s]* Accept only letters (A-Za-z) and spaces (\s) any time (*)
$ Matches end of input
Edit: If you don't want to match an empty string, then need to accept at least one character instead of any : ^(?! )[A-Za-z\s]+$
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