Validate FQDN Name without negative lookahead - regex

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.

Related

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.

regex - any number of digits + digit or [a-z]

I am trying to write a regular expresion that checks if a string starts with a number of digits (at least one), and then immediately ends with a single letter or a digit.
So:
29c is fine
29 is fine
2425315651252fsaw fails
24241jl.421c fails
c fails
The regex I have so far is (^\d+)([a-z]{1}|\d) which passes the 29, 20c, but also passes stuff like 29cdsd.
What am I doing wrong?
Your (^\d+)([a-z]{1}|\d) passes 29cdsd because it matches 1 or more digits at the start of the string followed with 1 letter or 1 digit, and allows anything right after.
Use
^[0-9]+[a-z0-9]?$
See regex demo
Details
^ - start of string
[0-9]+ - any 1 or more digits
[a-z0-9]? - 1 or 0 lowercase ASCII letters or digits
$ - end of string.
This should follow your rules exactly.
^\d+[a-z]?$
if "any number of digits" might be zero ^\d*\w$
You could add an anchor $ to assert the end of the line and you can omit the {1} part:
^(\d+)([a-z]|\d)$
In your regex you are matching a minimum of 2 characters .If you don't need the capturing groups, this could also be written as:
^\d+[a-z\d]$
Regex demo
That would match:
^ Assert the start of the string
\d+ Match 1+ digits
[a-z\d] A character class which matches a-z or a digit
$ Assert the end of the string
^ - start of string
\d* - any amount of digits, 0 or more
[a-zA-z] - a lowercase and uppercase ASCII letters.
$ - end of string

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.

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

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.