Regex, must contain a number, 1-5 chars and letters and digits - regex

Specs:
1 - 5 chars length
Contains at least 1 number char
Only A-Z, a-z and 0-9 allowed
This is what I have sofar: ^[a-zA-Z0-9]{0,5}$
Problem I have this accepts 'AV' for example. It must contain a number to be valid.
https://regex101.com/r/i5VnXt/1

You need to add a positive lookahead (?=\D*\d) and use {1,5} limiting quantifier at the end to match 1 to 5 chars:
^(?=\D*\d)[a-zA-Z0-9]{1,5}$
See the regex demo
Details
^ - start of a string
(?=\D*\d) - a positive lookahead requiring a digit after 0 or more non-digit symbols
[a-zA-Z0-9]{1,5} - 1 to 5 (due to {1,5} limiting quantifier) consecutive alphanumeric chars (ASCII letters or digits)
$ - end of string.

Related

How to sum characters and digits with regex?

I have a situation with a pattern that always has length = 6, stating with characters and finishings with numbers. I may have 2 to 5 characters, completing length to 6.
Examples:
ABCDE1
ABCD12
ABC123
AB1234
Today we use a Regex like this ([A-Z]{4}\d{2}|[A-Z]{3}\d{3}|[A-Z]{5}\d{1}) but it can grow, and make this solution turn into a monster.
There is a way where I can get how many characters I have to set how many digits I should have?
OBS: We always have characters and then digits, never mixed up.
You can assert 6 chars A-Z or digits. Then start the match with 2-5 chars A-Z and match 1+ digits.
^(?=[A-Z0-9]{6}$)[A-Z]{2,5}\d+$
^ Start of string
(?=[A-Z0-9]{6}$) Positive lookahead, assert 6 chars either A-Z or a digit till end of string
[A-Z]{2,5} Match 2-5 chars A-Z
\d+ Match 1+ digits
$ End of string
Regex demo

Username regex validation with an optional dot and specific string limit

I am writing an expression to validate a username with the following requirements:
length between 6 and 20,
must start with a letter,
numbers are allowed,
dot is optional or only one is allowed.
must not end with a dot
Tried with but didn't work:
^(([a-zA-Z])(\.{0,1})([a-zA-Z0-9]*)){6,20}$
another option
^([a-zA-Z]+([\\.]?)+([a-zA-Z0-9]*)){6,20}$
You may use an expression that will match a letter at the start and the 5 to 19 letters, digits or dots that should only appear in the middle of the string.
You may use
^(?=.{5,19}$)(?=[A-Za-z])[A-Za-z0-9]+(?:\.[A-Za-z0-9]+)*$
See the regex demo.
Details
^ - start of string
(?=.{5,19}$) - a positive lookahead that matches a string that contains any five to nineteen characters other than line break chars
(?=[A-Za-z]) - the first char must be an ASCII letter
[A-Za-z0-9]+ - one or more letters or digits
(?:\.[A-Za-z0-9]+)* - 0 or more repetitions of
\. - a dot
[A-Za-z0-9]+ - one or more letters or digits
$ - end of string.

Validate FQDN Name without negative lookahead

I have found this link(www.regextester.com/103452) to validate the based on REGEX.
(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)
But there's one problem, Oracle doesn't recognize Positive/Negative Lookahead.
The first positive lookahead (?=^.{4,253}$) is easy, I can just verify the length of the string, but for the negative lookahead (?!-) I'm having some trouble figuring it out.
The ((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+ means 1 or more repetitions of:
(?!-)[a-zA-Z0-9-]{0,62} - 0 to 62 letters, digits or - with the first char of the sequence not equal to -
[a-zA-Z0-9] - a letter or digit
\. - a dot.
Rephrasing: there can be 1 to 63 letters, digits or - with no - as first or last char of the sequence before ..
Use ([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+ instead, it will match 1 or more repetitions of:
[a-zA-Z0-9] - a letter or digit
([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])? - an optional sequence of 0 to 61 letters, digits or - and then 1 obligatory letter or digit should follow
\. - a dot.
So, it matches again 1 to 63 chars with no - at the start or end of the digit-letter-hyphen sequence.

conditional with no leading zeros in decimal number using regular expression

Below is my regular expression I am working with
^\d+\.?((25)|(5)|(75)|0+)?$ which should be allowing numbers like
0010,10.0,0.0,0,0.25,1.0,22,1.5,11.25,23.75,22.000 etc.
I don't want to allow like
0017.5,0010.0 etc with leading 0s in decimal numbers.
How can I make that?
Provided you may use lookaheads in the regex pattern, you may use
^(?!0+[1-9]\d*\.\d)\d+(?:\.(?:[27]?5|0+))?$
See the regex demo. The (?!0+[1-9]\d*\.\d) negative lookahead will fail the match if the strings starts with one or more zeros and the number is itself a number with a fractional part.
Regex details
^ - start of string
(?!0+[1-9]\d*\.\d) - a negative lookahead that fails the match if, immediately at the start of the string, there is
0+ - one or more 0 chars
[1-9] - a digit from 1 to 9
\d* - 0+ digits
\. - a dot
\d - a digit
\d+ - 1+ digits
(?:\.(?:[27]?5|0+))? - an optional string that matches 1 or 0 occurrences of:
\. - a dot
(?:[27]?5|0+) - 2 or 7 (optionally) and then 5, or one or more 0 chars
$ - end of string.
Give a range to the starting number instead of allowing any number and allow 0 or more numbers after that number but before the comma. Remove the ?..? quantifier because you want to make sure it is a decimal number. To account for whole numbers add |(\d+). And to account for numbers between 0 and 1, add ([0]\.((25)|(5)|(75)|0+)) This becomes:
^(([0]\.((25)|(5)|(75)|0+))|([1-9]\d*\.((25)|(5)|(75)|0+))|(\d+))$

What is the Regular Expression to allow only one hyphen(-) only between 2 letters or a letter and a number but not 2 numbers

I was using the below pattern.
/^[A-Za-z0-9]+(-[A-Za-z0-9]+)*$/.
What i need is that it should not be allowing the hyphen between 2 numbers.
I know that we have to make modification with 0-9, where we can restrict user from entering them twice.
The (?!.*[0-9]-[0-9]) lookahead after ^ will make sure there is no digit-digit pattern in the string. Also, if there must be 1 or 0 hyphens, replace * at the end with ? (0 or more occurrences).
Use
^(?!.*[0-9]-[0-9])[A-Za-z0-9]+(-[A-Za-z0-9]+)?$
See the regex demo.
Details
^ - start of string
(?!.*[0-9]-[0-9]) - a negative lookahead that fails the match if, after any 0+ chars other than line break chars, there is a digit, hyphen, digit pattern
[A-Za-z0-9]+ - 1 or more ASCII alphanumeric chars
(-[A-Za-z0-9]+)? - 1 or 0 sequences of:
- - a hyphen
[A-Za-z0-9]+ - 1 or more ASCII alphanumeric chars
$ - end of string.