Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you do this conditional situation with regular expression?

  1. Begins with alphanumeric ^[a-z0-9]
  2. Then followed by this optional dot \.?
  3. If there is a dot, then it MUST be followed by 2 to 4 alphabets [a-z]{2,4}
  4. It must be ends with an alphabet [a-z]$
  5. It has to be a dot and only two dots max.

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.

like image 806
Yoyo_c Avatar asked Jun 27 '11 13:06

Yoyo_c


People also ask

How do you create a condition in regex?

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.

Are there conditionals in regex?

Conditionals are one of the least used components of regex syntax. Granted, not all engines support them.

What does \\ mean in regular expression?

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.


2 Answers

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})$
like image 184
R. Martinho Fernandes Avatar answered Oct 21 '22 13:10

R. Martinho Fernandes


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.

like image 5
user812786 Avatar answered Oct 21 '22 11:10

user812786