I'm developing a docker project, need to write a regex to check repository name. Requirement as follow:
then, my regex is:
([a-z0-9]+(?:[._-][a-z0-9]+)*){2,255}
but, it can't be OK, when repository name is e-e_1.1
When I change it to:
[a-z0-9]+(?:[._-][a-z0-9]+)*{2,255}
it's OK.
Is there someone can explain? Thank you in advance.
As a result, broadly speaking, there are three types of regex engines: DFA (POSIX or not—similar either way) Traditional NFA (most common: Perl, . NET, PHP, Java, Python, . . . )
There are also two types of regular expressions: the "Basic" regular expression, and the "extended" regular expression.
The m flag is used to specify that a multiline input string should be treated as multiple lines. If the m flag is used, ^ and $ match at the start or end of any line within the input string instead of the start or end of the entire string.
\n. Matches a newline character. \r. Matches a carriage return character. \s.
In the ([a-z0-9]+(?:[._-][a-z0-9]+)*){2,255}
regex, the limiting quantifier {2,255}
is applied to the whole pattern inside Group 1 ([a-z0-9]+(?:[._-][a-z0-9]+)*
). It means it can be repeated 2 to 255 times. It does not mean the whole string length is restricted to 2 to 255 characters.
Now, your [a-z0-9]+(?:[._-][a-z0-9]+)*{2,255}
regex can match unlimited characters, too, because the string matched with [a-z0-9]+
can have 1 or more characters. (?:[._-][a-z0-9]+)*
can match zero or more characters. The limiting quantifier {2,255}
does not work here at all the way you need.
To restrict the length of the input string to 2 to 255 characters, you will have to use a lookahead anchored at the start:
^(?=.{2,255}$)[a-z0-9]+(?:[._-][a-z0-9]+)*$
^^^^^^^^^^^^^
The (?=.{2,255}$)
lookahead will be executed only once at the beginning of the string and a match will only be found if the condition inside the lookahead is met: there must be 2 to 255 characters (.
matches any characters other than a newline, but it is not important as you only allow specific characters in the matching pattern later) up to the end of the string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With