Regex pattern to match username requirements - regex

Can someone please provide me with a regex pattern to match these requirements?
between 3 and 20 characters
begins with a letter
cannot end in a period(.)
can contain: a-z, 0-9, period(.), hyphen(-), underscore(_)
I'm new to regex. I've tried ^[[:alpha:]][[:alnum:]_.-]+$, given to me by someone, and I started to do my own with [a-z,A-Z,.]{3,20}[0-9]*
I will be using this in JavaScript but so far I've just been testing at regexr.com because it is convenient.

You can use /^[a-z][\w.-]{1,18}[\w-]$/i as a pattern. A little breakdown:
^ is an anchor for the start of the string, as we want to check the whole string
[a-z] is a character class matching letters a-z, lowercase and also uppercase due to the i-Modifier. This is used for your begins with a letter condition
[\w.-]{1,18} is a character class matching letters, numbers, underscore (= \w), dot and hyphen. It is repeated one to eighteen times to fit between 3 and 20 characters (+ 2 characters at start and end)
[\w-] is basically the same character class, but without the dot, to fit cannot end in a period(.)
$ is an anchor for the end of the string

Related

Unicode regex that do not match any letter nor any punctuation sign

I am trying to create a unicode regex that matches every character except for a letter (of any language) and the punctuation signs .;:?!.
So for example the string
abcd 123 kjd ¤%/(" .?:!
should only match the bold parts below
abcd 123 kjd ¤%/(" .?:!
I know that \P{L}+ matches everything except a letter and \P{P}+ matches everything except a punctuation sign. How do I combine this two regex string to one? I have tried simply putting the together \P{L}+\P{P}+ but this does not give the required match. I have also tried writing [^.;:?!]\P{L}+ but this does not work either.
How do I combine one or more unicode regex or is there a better regex that achieves my requirement?
Using \P{L}+\P{P}+ will match 1+ times the opposite of any letter followed by 1+ times the opposite of any punctuation mark.
The pattern [^.;:?!]\P{L}+ matches 1 time any character other than the listed followed by 1+ times the opposite of any letter.
What you could do is add \p{L} (which will match any kind of letter) to the negated character class. As advised by Wiktor Stribiżew, you can add \p{Z} to match any kind of whitespace.
[^\p{Z}\p{L}.;:?!]
Regex demo

Regex for name type

I am working on regex with the following conditions:
Must contain from 1 to 63 alphanumeric characters or hyphens.
First character must be a letter.
Cannot end with a hyphen or contain two consecutive hyphens.
I am able to get the regex like:
^[a-zA-Z0-9](?!.*--)[a-zA-Z0-9-]{0,61}[A-Za-z0-9]$
But it fails on the length constraint as well as allows patterns like "a-". How can I meet the conditions?
I would phrase your requirements as:
^(?=.{1,63}$)(?!.*--)[a-zA-Z]([a-zA-Z0-9\-]*[a-zA-Z0-9])?$
Demo
Here is a brief explanation of what each part of the above regex does:
^ from the start of the match
(?=.{1,63}$) assert that the string is between 1 63 characters
(?!.*--) assert that two hyphens do not appear together anywhere
[a-zA-Z] first character is a letter (mandatory in all matches)
([a-zA-Z0-9\-]*[a-zA-Z0-9])?
The final portion says to match a final character which is alphanumeric, but not dash, possibly preceded by alphanumeric characters or dash.
My take on this would be:
^[A-Za-z](?!.*?--)[A-Za-z0-9\-]{0,62}(?<!-)$
Try it out here
Explanation:
^ - Matches the start of the string.
[A-Za-z] - Matches the first letter.
(?!.*?--) - Ensures that there are no two consecutive hyphens in the rest of the string.
[A-Za-z0-9\-]{0,62} - Matches the remaining alphanumeric and hyphen characters.
(?<!-) - Ensures that the string doesn't end with a hyphen.
$ - Matches the end of the string.

Regexp at least 8 symbols and only one uppercase character

I need a regular expression for a string with has at least 8 symbols and only one uppercase character. Java
For example, it should match:
Asddffgf
asdAsadasd
asdasdaA
But not:
adadAasdasAsad
AsdaAadssadad
asdasdAsadasdA
I tried this: ^[a-z]*[A-Z][a-z]*$ This works good, but I need at least 8 symbols.
Then I tried this: (^[a-z]*[A-Z][a-z]*$){8,} But it doesn't work
^(?=[^A-Z]*[A-Z][^A-Z]*$).{8,}$
https://regex101.com/r/zTrbyX/6
Explanation:
^ - Anchor to the beginning of the string, so that the following lookahead restriction doesn't skip anything.
(?= ) - Positive lookahead; assert that the beginning of the string is followed by the contained pattern.
[^A-Z]*[A-Z][^A-Z]*$ - A sequence of any number of characters that are not capital letters, then a single capital letter, then more non capital letters until the end of the string. This insures that there will be one and only one capital letter throughout the string.
.{8,} - Any non-newline character eight or more times.
$ - Anchor at the end of the string (possibly unnecessary depending on your requirements).
In your first regex ^[a-z]*[A-Z][a-z]*$ you could append a positive lookahead (?=[a-zA-Z]{8,}) right after the ^.
That will assert that what follows matches at least 8 times a lower or uppercase character.
^(?=[a-zA-Z]{8,})[a-z]*[A-Z][a-z]*$

Regex to match string containing letters and only underscore

Well my question is simple, I want to match a string with following attributes
No white space
Must start with a letter
Must not contain any other special characters other than underscore
May contain numbers
Please help in creating such a regex.
^[a-zA-Z][a-zA-Z0-9_]*$
Dissecting it:
^ start of line/string
[a-zA-Z] starts with a letter
[a-zA-Z0-9_]* followed by zero or more letters, underscores or digits.
$ end of line/string
If you need to consider Unicode, then the following is probably more sane:
^\p{L}[\p{L}\p{Nd}_]*$
This will match not only ASCII letters and digits but across all scripts that are supported by Unicode. Digits are restricted to decimal digits, only, so you won't get Roman numerals.
/^[a-zA-Z]\w*$/
a-Z - start with letter
\w - all leters, numbers and underscore

Regex to match 4 groups of letters/numbers, separated by hyphens

I need a regular expression that will match this pattern (case doesn't matter):
066B-E77B-CE41-4279
4 groups of letters or numbers 4 characters long per group, hyphens in between each group.
Any help would be greatly appreciated.
^(?:\w{4}-){3}\w{4}$
Explanation:
^ # must match beginning of string
(?: # make a non-capturing group (for duplicating entry)
\w{4} # a-z, A-Z, 0-9 or _ matching 4 times
- # hyphen
){3} # this group matches 3 times
\w{4} # 4 more of the letters numbers or underscore
$ # must match end of string
Would be my best bet. Then you can use Regex Match (static).
P.S. More info on regex can be found here.
P.P.S. If you don't want to match underscores, the \w above can be replaced (both times) with [a-zA-Z0-9] (known as a class matching lowercase and uppercase letters and numbers). e.g.
^(?:[a-zA-Z0-9]{4}-){3}[a-zA-Z0-9]{4}$
Try:
[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}
With such a small sample of data, it's not easy to be certain what you actually want.
I'm going to assume that all the characters in that string are hex digits, and that's what you need to search for.
In that case, you would need a regular expression something like this:
^[a-f0-9]-[a-f0-9]-[a-f0-9]-[a-f0-9]$
If they can be any letter, then replace the fs with zs.
Oh, and use myRE.IgnoreCase = True to make it case insensitive.
If you need further advice on regular expressions, I'd recommend http://www.regular-expressions.info/ as good site. They even have a VB.net-specific page.
Assuming from your example:
There are four groups of letters, separated by dashes.
Each group is four letters.
The letters are hexadecimal digits.
This pattern would match that:
^[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{4}$
Note that ^ and $ match the beginning and end of the string, which is important if you want to match the entire string and not check if the pattern occurs inside a string.
You could also make use of the repetitions in the pattern:
^(?:[\dA-F]{4}-){3}[\dA-F]{4}$