Validating usernames using regular expressions - regex

I have to validate a username in reactJs.
The conditions are-
It should be alphanumeric
Should be greater than 5 characters and less than 11 characters
Should not start with a digit
My solution is not working:
value.match(/^[a-zA-Z][a-zA-Z0-9]{6,10}/)

You just need to change {6,10} to {5,9} since [a-zA-Z] has already represent a character
value.match(/^[a-zA-Z][a-zA-Z0-9]{5,9}$/)

You are already matching 1 character in the first character class [a-zA-Z].
To match greater than 5 characters and less than 11 characters you could use {5,9} as a quantifier for the second character class and assert the end of the line $ to prevent match from returning the first 9 characters when the string is longer than 9 characters.
^[a-zA-Z][a-zA-Z0-9]{5,9}$
Regex demo
const strings = [
"A123456789BBBBBBB",
"A123456789"
];
let pattern = /^[a-zA-Z][a-zA-Z0-9]{5,9}$/;
strings.forEach((value) => {
console.log(value.match(pattern));
});

One method is to use lookaheads to validate the string with some rules.
You could use this pattern ^(?=[a-zA-Z0-9]{5,11}$)(?!\d).+.
This (?=[a-zA-Z0-9]{5,11}$) assures, that what follows beginning of a string ^ is 5 to 11 alphanumerics.
Second, negative, lookahead is (?!\d) to prevent from matching, when digit is first character in a string.
Demo
Read this for reference.

Related

Regex to match a string containing 14 digits and 1 character at any position

I need a regular expression that matches a string of 15 characters where 14 of them are digits and 1 is a character. The character can be in any position of the string.
I have the following long regex:
^.\d{14}|\d{1}.\d{13}|\d{2}.\d{12}|\d{3}.\d{11}|\d{4}.\d{10}|\d{5}.\d{9}|\d{6}.\d{8}|\d{7}.\d{7}|\d{8}.\d{6}|\d{9}.\d{5}|\d{10}.\d{4}|\d{11}.\d{3}|\d{12}.\d{2}|\d{13}.\d{1}|\d{14}.$
Can it be simplified?
Here is a sample match: 1000-1234567890
(?=^.{15}$)\d{0,14}\D\d{0,14}$
First check the string is 15 characters long, then has 0-14 digits, one non-digit, then 0-14 digits.
This isn't exactly the same as the original regex, which allows 15 digits in a row. To get that, simply change \D to .
We can use a lookaround trick here:
^(?!(?:.*\D){2})(?=.{15}$)\d*\D\d*$
This regex pattern says to match:
^ from the start of the string
(?!(?:.*\D){2}) assert that 2 or more non digits do NOT occur (implying at most 1 occurs)
(?=.{15}$) assert length is 15 characaters
\d*\D\d* then a non digit surrounded, possibly, on either side by numbers
$ end of the string

Regex for a string with alpha numeric containing a '.' character

I have not been able to find a proper regex to match any string not starting and ending with some condition.
This matches
AS.E
23.5
3.45
This doesn't match
.263
321.
.ASD
The regex can be alpha-numeric character with optional '.' character and it has to be with in range of 2-4(minimum 2 chars & maximum 4 chars).
I was able to create one ->
^[^\.][A-Z|0-9|\.]{2,4}$
but with this I couldn't achieve mask '.' character at the end of regex.
Thanks.
Maybe not the most optimized but a working one. Created step by step:
The first character should be alphanumeric
^[a-zA-Z0-9]
0, 1 or 2 character alphanumeric or . but not matching end of string
[a-zA-Z0-9\.]{0,2}
an alphanumeric character matching end of string
[a-zA-Z0-9]$
Concatenate all of this to obtain your regex
^[a-zA-Z0-9][a-zA-Z0-9\.]{0,2}[a-zA-Z0-9]$
Edit: This regex allows multiple dots (up to 2)
If I guessed correctly, you want to match all words that are
Between 2 and 4 characters long ...
... and start and end with a character from [A-Z0-9] ...
... and have characters from [A-Z0-9.] in the middle ...
... and are not preceded or followed by a ..
Try this regex to match all these substrings in a text:
(?<=^|[^.])[A-Z0-9][A-Z0-9.]{0,2}[A-Z0-9](?=$|[^.])
However, note that this will match the AA in .AAAA.. If you don't want this match, then please give more details on your requirements.
When you are only interested in the number of matches, but not the matched strings, then you could use
(^|[^.])[A-Z0-9][A-Z0-9.]{0,2}[A-Z0-9]($|[^.])
If you have one string, and want to know whether that string completely matches or not, then use
^[A-Z0-9][A-Z0-9.]{0,2}[A-Z0-9]$
If there may be at most one . inside the match, replace the part [A-Z0-9.]{0,2} with ([A-Z0-9]?[A-Z0-9.]?|[A-Z0-9.]?[A-Z0-9]?).
You can use this pattern to match what you say,
^[^\.][a-zA-Z0-9\.]{2,4}[^\.]$
Check the result here..
https://regex101.com/r/8BNdDg/3

preg_match allow letters and numbers but at least 1 letter

I have the following preg_match statement that allows an input to be either letters a to z and numbers 0 to 9 only and it must be between 5 and 30 characters in length.
However, how would I also make sure that the input is not all numerics? For example it must contain at least 1 a to z letter?
if (preg_match ('/^[a-zA-Z0-9\'.]{5,30}$/i', $str['user'])) {
}
You wrote you have a regex that allows an input to be either letters a to z and numbers 0 to 9 only, but actually ^[a-zA-Z0-9\'.]{5,30}$ matches a string of 5 to 30 ASCII letters, digits and also ' or ..
If you just want to make your regex match what it matches, but not a string that only consists of digits, use a (?!\d+$) lookahead:
'/^(?!\d+$)[a-z0-9\'.]{5,30}$/i'
^^^^^^^^
Note you do not need to specify the A-Z or a-z range if you use a i case insensitive flag (just either of them is enough).
The (?!\d+$) is a negative lookahead that fails the match if there are one or more digits followed by the end of string ($) immediately to the right of the current location (which is the start of the string).
See the regex demo.
If you do not want to match a string that only consists of dots, or ', or digits, use
'/^(?!(?:\d+|([\'.])\1*)$)[a-z0-9\'.]{5,30}$/i'
See another demo.

Regular Expression begining of string with special characters

Using this for an example string
+$43073$7
and need the 5 number sequence from it I'm using the Regex expression
#"\$+(?<lot>\d{5})"
which is matching up any +$ in the string. I tried
#"^\$+(?<lot>\d{5})"
as the +$ are always at the beginning of the string. What will work?
If you use anchor ^, you need to include the + symbol at the first and don't forget to escape it because + is a special meta character in regex which repeats the previous token one or more times.
#"^\+\$(?<lot>\d{5})"
And without the anchor, it would be like
#"\$(?<lot>\d{5})"
And get the 5 digit number you want from group index 1.
DEMO
I would match what you want:
\d+
or if you only want digits after "special" characters at the start of input:
^\W+(\d+)
grabbing group 1

Regex for 8-digit expression without hyphen or letters

Currently, I have this:
Regex folderRegex = new Regex(#"^.{8})([0-9]+)?[1-9]+([0-9]+)?$");
I need the string to have exactly 8 digits. Without hyphens or letters. Would my regex do that?
I think you need a very simple pattern.
^\d{8}$
Short description
Assert position at the beginning of the string ^
Match a single digit 0..9 \d
Exactly 8 times {8}
Assert position at the end of the string (or before the line break at the end of the string, if any) $
You can also do the same thing using a character class with a grouping range.
Regex folderRegex = new Regex(#"^[0-9]{8}$");
Regular expression:
[0-9]{8} any character of: '0' to '9' (8 times)
Assuming from what is given in your question that you do not want your string of eight digits to start with a 0, this should do:
^[1-9]\d{7}$