^[a-z0-9]
\.?
[a-z]{2,4}
[a-z]$
it's like domain names:
yahoo.co.uk
or yahoo.com
, but you cannot do this yahoo.co.u
or this yahoo.co.
, yes something like that.
A special construct (? ifthen|else) allows you to create conditional regular expressions. If the if part evaluates to true, then the regex engine will attempt to match the then part. Otherwise, the else part is attempted instead.
Conditionals are one of the least used components of regex syntax. Granted, not all engines support them.
You also need to use regex \\ to match "\" (back-slash). Regex recognizes common escape sequences such as \n for newline, \t for tab, \r for carriage-return, \nnn for a up to 3-digit octal number, \xhh for a two-digit hex code, \uhhhh for a 4-digit Unicode, \uhhhhhhhh for a 8-digit Unicode.
You can group the optional dot with the 2-4 characters that must follow it: (\.[a-z]{2,4})
. That said, you will have either none, or up to two of these groups of dot + alphabetic characters (\.[a-z]{2,4}){0,2}
.
The must end with [a-z]
part, you can check with a positive lookbehind (?<=[a-z])
giving this as the full regex:
^[a-z0-9]+(\.[a-z]{2,4}){0,2}(?<=[a-z])$
This will work in Perl and PHP regexes (PCRE), but not in JavaScript, because it does not support lookbehind. In this specific case, you can work around that limitation.
If there is at least one dot, there's already a guarantee that it will end in [a-z]
, because that test is in the group that the dot is a part of. If there is no dot, you need to force a [a-z]
at the end. To do this you can turn the one-or-more quantifier (+
) into a zero-or-more (*
) and force the end to be an [a-z]
when there are no "dot groups". When there are dot groups, you can keep the same pattern, but now with at least one mandatory dot.
^([a-z0-9]*[a-z]|[a-z0-9](+\.[a-z]{2,4}){1,2})$
This checks for a string that begins with [a-z][0-9]
and then contains one or two dots followed by 2/4 alphabets. It works (in Python, at least) for the examples you provided (true for yahoo.co.uk
and yahoo.com
, false for yahoo.co.u
and yahoo.co.
)
^[a-z0-9]+(\.[a-z]{2,4}){1,2}$
Edit - upon re-reading, I think you may want this instead:
^[a-z0-9]*([a-z0-9](\.[a-z]{2,4}){1,2}$|[a-z]$)
This will match strings (in addition to the above) that do not include dots but end with a letter, such as yahoo
, but not yahoo2
.
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