Allowing dash in string but not several consecutive dashes - php

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.

Related

Regex Expression Errors [duplicate]

basically I need a preg_match that ensures that only one space character is allowed between each word (IF there is more than one word).
My existing rule is:
(^[A-Za-z\s]+$)
Thanks.
It depends what you mean by "word", but assuming you mean letters in A-Z or a-z you can try this:
^[a-zA-Z]+( [a-zA-Z]+)*$
Note that \s does not mean the space character - it means any whitespace, including a new line.
/^[\S]+\s?[^\s]?/i
Will search for any word with one space after it and not a space after that one space
/\s?\S+\s/
Will search for any character with whitespace on either side or only on the end.
i think to specify a number of occurences to your match you put i.e{1} {1,5} (to match only 1 or between 1 and 5 inclusive) so something like...
^[A-Za-z]+( {1})$

Regex for the following condition

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]+\.?)+$

How do you do this conditional situation with regular expression?

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]$

PHP Simple PCRE regex to only allow 1 dot or none?

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.

PHP preg_match needed to ensure only ONE space character is allowed between words

basically I need a preg_match that ensures that only one space character is allowed between each word (IF there is more than one word).
My existing rule is:
(^[A-Za-z\s]+$)
Thanks.
It depends what you mean by "word", but assuming you mean letters in A-Z or a-z you can try this:
^[a-zA-Z]+( [a-zA-Z]+)*$
Note that \s does not mean the space character - it means any whitespace, including a new line.
/^[\S]+\s?[^\s]?/i
Will search for any word with one space after it and not a space after that one space
/\s?\S+\s/
Will search for any character with whitespace on either side or only on the end.
i think to specify a number of occurences to your match you put i.e{1} {1,5} (to match only 1 or between 1 and 5 inclusive) so something like...
^[A-Za-z]+( {1})$

Categories