How do you make this conditional with regexp?

  • Starts with an alphanumeric ^[a-z0-9]
  • Then follows this optional dot \.?
  • If there is a dot, then 2 to 4 alphabets MUST follow it [az]{2,4}
  • It must end with the alphabet [az]$
  • There must be a point and only two points max.

it's like domain names:

yahoo.co.uk or yahoo.com , but you cannot do it yahoo.co.u or it is yahoo.co. yes something like that.

+6
source share
4 answers

You can group an optional dot with 2-4 characters that should follow it: (\.[az]{2,4}) . However, you will either have no or up to two of these groups of characters with a dot + alphabet (\.[az]{2,4}){0,2} .

The required end with the [az] , you can check with a positive lookbehind (?<=[az]) , indicating this as a complete regex:

 ^[a-z0-9]+(\.[az]{2,4}){0,2}(?<=[az])$ 

This will work in Perl and PHP regexes (PCRE), but not in JavaScript, because it does not support lookbehind. In this particular case, you can circumvent this limitation.

If there is at least one point, there is already a guarantee that it will end on [az] , because this test is in the group in which the point is located. If there is no point, you need to force press [az] at the end. To do this, you can turn the quantifier of one or more ( + ) into zero or more ( * ) and force the end to be [az] when there are no "point groups". When there are groups of points, you can save the same pattern, but now with at least one required point.

 ^([a-z0-9]*[az]|[a-z0-9](+\.[az]{2,4}){1,2})$ 
+7
source

This checks the line starting with [az][0-9] and then contains one or two points followed by 2/4 alphabets. It works (at least in Python) for the examples you provided (true for yahoo.co.uk and yahoo.com , false for yahoo.co.u and yahoo.co. )

 ^[a-z0-9]+(\.[az]{2,4}){1,2}$ 

Edit - when re-reading, I think you might need this:

 ^[a-z0-9]*([a-z0-9](\.[az]{2,4}){1,2}$|[az]$) 

This will correspond to lines (in addition to above) that do not include yahoo2 , but end with a letter, for example yahoo , but not yahoo2 .

+5
source

Try the following:

 ^[a-z0-9](\.[az]{2,4}|.*[az]$) 
-1
source
 ^[a-z0-9](?=[^.]*\.[^.]+$|[^.]*\.[^.]\.[^.]+$)(\.(?=[az][az]){1,2}).*[az]$ 
-1
source

Source: https://habr.com/ru/post/891451/


All Articles