Begins with alphanumeric ^[a-z0-9]
Then followed by this optional dot \.?
If there is a dot, then it MUST be followed by 2 to 4 alphabets [a-z]{2,4}
It must be ends with an alphabet [a-z]$
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.
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.
Try this:
^[a-z0-9](\.[a-z]{2,4}|.*[a-z]$)
^[a-z0-9](?=[^.]*\.[^.]+$|[^.]*\.[^.]\.[^.]+$)(\.(?=[a-z][a-z]){1,2}).*[a-z]$
Related
I want to construct a pattern that identifies the valid domain name. A valid domain name has alphanumeric characters and dashes in it. The only rule is that the name should not begin or end with a dash.
I have a regular expression for the validation as ^\w((\w|-)*\w)?$
However the expression is validating the strings with underscores too (for ex.: cake_centre) which is wrong. Can anyone tell me why this is happening and how it can be corrected?
P.S.: I am using preg_match() function in PHP for checking the validation.
The metacharacter \w includes underscores, you can make a character class that will allow your listed requirements:
[a-zA-Z\d-]
or per your regex:
^[a-zA-Z\d]([a-zA-Z\d-]*[a-zA-Z\d])?$
(Also note the - position in the character class is important, a - at the start or end is the literal value. If you have it in the middle it can create a range. What special characters must be escaped in regular expressions?)
Underscores are being validated because they are part of the \w character class. If you want to exclude it, try:
/^[a-z0-9]+[a-z0-9\-]*[a-z0-9]+$/i
Here is the regexp with lookaround approach
(?<!-)([a-zA-Z0-9_]+)(?!-)
regexp pattern is created in 3 groups
First group ^(?<!-) is negetive look back to ensure that matched chars does not have dash before
Second group ([a-zA-Z0-9_]+) give matching characters
Third group (?!-) is negetive lookahead to ensure match is not ending with dash
I need a small help with regex for the following
Alphanumeric with only lower case alphabets allowed
Starts with number or alphabet
Allows period (.)
Doesn't allow consecutive periods No ..
Doesn't allow any other special characters
Thanks,
-GM
^(?![^.]*\.\.)[a-z0-9][a-z0-9.]*$
The negative lookahead at the beginning covers your 4th requirement, everything else should be pretty straightforward. ^ and $ are beginning and end of string anchors, the character classes enforce the requirement that only lowercase letters, numbers, and . are allowed.
To add the length constraint (between 6 and 16 characters) just change the * to {5,15}. * means "repeat the previous element zero or more times", {n,m} means "repeat the previous element between n and m times (inclusive)". The reason {5,15} is used instead of {6,16} is that one character is already consumed by the first character class. Here is the end result:
^(?![^.]*\.\.)[a-z0-9][a-z0-9.]{5,15}$
Here's some assistance without giving away the answer, as you'll learn the most.
To match from a certain combination of characters, e.g. alphanumeric, use character classes, e.g. [a-z0-9]. Note that this expression matches exactly one character. You must use quantifiers to match more than one, e.g. +.
To "start" or "end" with something, you must use anchors, ^ and $, before the first or after the last character, respectively. (Watch out, though. In a character class, the ^ inverts the character class.)
In regex, . has a special meaning as a wildcard (matching any character besides newline characters). Therefore you have to escape them, \., to select the literal dot. Another way to escape the dot is to put it in a character class: [.].
Non-consecutiveness is trickier. You will need to look up more information about negative lookahead assertions (or lookaround assertions in general).
All the bolded words are terms you can Google to learn.
I'd say something along those lines: /^[a-z0-9]+(\.[a-z0-9]+)*\.?$/ (suppose that the line can end with a period)
Use this if the string may not end with a period:
/^[a-z0-9]+(\.[a-z0-9]+)*$/
or this if it may:
/^[a-z0-9]+(\.[a-z0-9]+)*\.?$/
This should be the best
^([a-z0-9]+\.?)+$
in my program php, I want the user doesn't enter any caracters except the alphabets
like that : "dgdh", "sgfdgdfg" but he doesn't enter the numbers or anything else like "7657" or "gfd(-" or "fd54"
I tested this function but it doesn't cover all cases :
preg_match("#[^0-9]#",$_POST['chaine'])
how can I achieve that, thank you in advance
The simplest can be
preg_match('/^[a-z]+$/i', $_POST['chaine'])
the i modifier is for case-insensitive. The + is so that at least one alphabet is entered. You can change it to * if you want to allow empty string. The anchor ^ and $ enforce that the whole string is nothing but the alphabets. (they represent the beginning of the string and the end of the string, respectively).
If you want to allow whitespace, you can use:
Whitespace only at the beginning or end of string:
preg_match('/^\s*[a-z]+\s*$/i', $_POST['chaine'])
Any where:
preg_match('/^[a-z][\sa-z]*$/i', $_POST['chaine']) // at least one alphabet
Only the space character is allowed but not other whitespace:
preg_match('/^[a-z][ a-z]*$/i', $_POST['chaine'])
Two things. Firstly, you match non-digit characters. That is obviously not the same as letter characters. So you could simply use [a-zA-Z] or [a-z] and the case-insensitive modifier instead.
Secondly you only try to find one of those characters. You don't assert that the whole string is composed of these. So use this instead:
preg_match("#^[a-z]*$#i",$_POST['chaine'])
Only match letters (no whitespace):
preg_match("#^[a-zA-Z]+$#",$_POST['chaine'])
Explanation:
^ # matches the start of the line
[a-zA-Z] # matches any letter (upper or lowercase)
+ # means the previous pattern must match at least once
$ # matches the end of the line
With whitespace:
preg_match("#^[a-zA-Z ]+$#",$_POST['chaine'])
This is a regex where string must start and end with an alphanumeric character and can contain alphanumeric characters and dashes.
/^[a-zA-Z0-9]{1}[a-zA-Z0-9\-]+[a-zA-Z0-9]{1}$/
How can I make sure that the consecutive dashes are not allowed? for example:
should allow: some-string
should NOT allow: some--string
Thanks
Edit: I want to allow several dashes, just not consecutively. for example "some-thing-here" is OK, and "some--thing" is NOT.
No need for complicated patterns with optional dashes just use this:
/^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$/
See it here on Regexr
Start with at least one alphanumeric. Then there can be a dash followed by at least one alphanumerics 0 ore more times.
Something like:
/^[a-zA-Z0-9]{1}\-?([a-zA-Z0-9]+\-?)*[a-zA-Z0-9]{1}$/
The key part being \-?([a-zA-Z0-9]+\-?)* which makes it read "a letter/number, optional dash, any amount of (some letters/numbers, optionally followed by a dash), ending in a letter or number."
This allows some-string, my-double-dash-string but not my-double--dash-string.
/^[a-zA-Z0-9]+-?[a-zA-Z0-9]+$/
? for "occuring zero or one time"
To allow multiple pieces separated with one dash, change it to:
/^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$/
The (...)* says that the last group (starting with a -) may be repeated zero or more times.
/^[a-zA-Z0-9]+-?[a-zA-Z0-9]+$/
Should work I believe. You don't need to differentiate the initial and ending group of the alphanumberical characters. Simply expect each dash to be surronded by such character.
I'm trying to create a regex for alias validation:
And I'm allowing letters, numbers and 1 dot.
I have done the following:
/^[a-z0-9\\.]+$/i
However it allows more then 1 dot?
This should do it:
/^(?:\.[a-z0-9]+|[a-z0-9]+(?:\.[a-z0-9]*)?)$/i
This allows the string to either:
start with one dot that is followed by at least one alphanumeric character, or
start with one or more alphanumeric character that may be followed by one dot and zero or more alphanumeric characters.
I think it is not a good idea to allow a dot as a first or last character, in that case:
/^[a-z0-9]+\.?[a-z0-9]+$/i
try this:
^(?:[a-z0-9]+\.?[a-z0-9]*|[a-z0-9]*\.?[a-z0-9]+)$
places the dot in the center, then allows it to be surrounded on either side.