I want a regular expression in PHP which will check for all caps the string.
If the given string contains all capital letters irrespective of numbers and other characters then it should match them.
Since you want to match other characters too, look for lowercase letters instead of uppercase letters. If found, return false. (Or use tdammers' suggestion of a negative character class.)
return !preg_match('/[a-z]/', $str);
You can also skip regex and just compare strtoupper($str) with the original string, this leaves digits and symbols intact:
return strtoupper($str) == $str;
Both don't account for multi-byte strings though; for that, you could try adding a u modifier to the regex and using mb_strtoupper() respectively (I've not tested either — could someone more experienced with Unicode verify this?).
if (preg_match('/^[^\p{Ll}]*$/u', $subject)) {
# String doesn't contain any lowercase characters
} else {
# String contains at least one lowercase characters
}
\p{Ll} matches a Unicode lowercase letter; [^\p{Ll}] therefore matches any character that is not a lowercase letter.
Something like this maybe:
'/^[^a-z]*$/'
The trick is to use an exclusive character class: this one matches all characters that are not lower-case letters. Note that accented letters aren't checked.
Related
Here is my code:
function isValid($string) {
return strlen($string) >= 6 &&
strlen($string) <= 40 &&
preg_match("/\d/", $string) &&
preg_match("/[a-zA-Z]/", $string);
}
// Negative test cases
assert(!isValid("hello"));
// Positive test cases
assert(isValid("abcde2"));
As you see, my script validates a string based on 4 conditions. Now I'm trying to develop this one:
preg_match("/[a-zA-Z]/", $string)
This condition returns true just for English letters. How can I also add other letters like ا ب ث چ. Well how can I do that?
Note: Those characters aren't Arabic, they are Persian.
To match either an English or Persian letter, you may use
preg_match('/[\x{0600}-\x{06FF}A-Z]/iu', $string)
The \x{0600}-\x{06FF} range is supposed to match all Persian letters. The A-Z range will match all ASCII letters (both upper- ans lowercase since the /i case insensitive modifier is used). The /u modifier is necessary since you are working with Unicode characters.
Also, use mb_strlen rather than strlen when checking a Unicode string length, it will count the Unicode code points correctly.
As for
Your password should be containing at least a letter (that letter can be in any language
You need to use
preg_match('/\p{L}/u', $string)
or
preg_match('/\p{L}\p{M}*+/u', $string)
^^^^^^^^^^^^
that will match any letter (even the one with a diacritic after it). \p{L} matches any base Unicode letter, and \p{M}*+ will possessively match 0+ diacritics after it. If the match value is not used, /\p{L}/u will suffice for the check.
How can I check if a string starts with a capital letter, in cases where the first letter might not be in A-Z range but from other languages also AND simultaneously if the first character is not a number?
examples:
"This is string" - match
"this is string" - not match
"5 not good" - not match
"Увеличи starts capital" - match
"мащабиране no capital" - not match
in php:
if (preg_math('/??/i', $str) ) {
echo 'yeeee haaa';
}
Use this regex:
preg_match('#^\p{Lu}#u', $str)
\p{Lu} will match any character that has Unicode character property of uppercase letter.
Demo on regex101 (please ignore the flags m and g, they are for demonstration purpose only)
When you are dealing with a Unicode string (or more specifically, in the case of preg_ functions, the Unicode string must be in UTF-8 encoding), you must always use the u flag to make the engine treat the input string and the pattern with character semantics. Otherwise, by default, preg_ functions treat the pattern and input string as an array of bytes and produce an unexpected result for characters outside the ASCII range.
What about:
if ($string{0} != mb_strtoupper($string{0}, 'UTF-8')) {
// not uppercase
}
Maybe something like:
if (strtoupper($str[0]) == $str[0])
{
echo "match";
}
or:
if(ctype_upper($str[0]))
{
echo "match";
}
I'm not sure they would work with every character set.
Hi I need to use php's pregmatch to check a string is valid. In order to be valid the string needs to have at least one uppercase character, at least one lowercase character, and then at least one symbol or number
thanks
You can achieve this by using lookaheads
^(?=.*[a-z])(?=.*[A-Z])(?=.*[\d,.;:]).+$
See it here on Regexr
A lookahead is a zero width assertion, that means it does not match characters, it checks from its position if the assertion stated is true. All assertions are evaluated separately, so the characters can be in any order.
^ Matches the start of the string
(?=.*[a-z]) checks if somewhere in the string is a lowercase character
(?=.*[A-Z]) checks if somewhere in the string is a uppercase character
(?=.*[\d,.;:]) checks if somewhere in the string is a digit or one of the other characters, add those you want.
.+$ Matches the string till the end of the string
As soon as one of the Assertions fail, the complete regex fail.
If the match has to be in the order you've described, you could use
$result = preg_match('/[A-Z]+[a-z]+[\d!$%^&]+/', $string);
If the characters can be in any order I'm not so sure, without doing three separate checks like so:
$result = (preg_match('/[A-Z]+/', $string) && preg_match('/[a-z]+/', $string) && preg_match('/[\d!$%^&]+/', $string));
As people have pointed out below, you can do this all in one regular expression with lookaheads.
According to your request:
[A-Z]+ Match any uppercase char
[a-z]+ Match any lowercase char
[\d§$%&]+ Match a number or special chars (add more special if you need to)
The result would look like this: [A-Z]+[a-z]+[\d§$%&]+
This isn't ideal though. You might want to check Regexr and try what kind of regex fits your requirements.
If you want these not to be necessarily in order, you need a lookahead. The following expression will validate for at least one lower char, one upper char and one number:
$result = preg_match('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])', $string);
You can put a lot of special chars with the numbers, like this:
$result = preg_match('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9$%])', $string);
Im about to create a registration form for my website. I need to check the variable, and accept it only if contains letter, number, _ or -.
How can do it with regex? I used to work with them with preg_replace(), but i think this is not the case. Also, i know that the "ereg" function is dead. Any solutions?
this regex is pretty common these days.
if(preg_match('/^[a-z0-9\-\_]+$/i',$username))
{
// Ok
}
Use preg_match:
preg_match('/^[\w-]+$/D', $str)
Here \w describes letters, digits and the _, so [\w-]+ matches one or more letters, digits, _, and -. ^ and $ are so called anchors that denote the begin and end of the string respectively. The D modifier avoids that $ really matches the end of the string and is not followed by a line break.
Note that the letter and digits that are matched by \w depend on the current locale and might match other letter or digits than just [a-zA-Z0-9]. So if you just want these, use them explicitly. And if you want to allow more than these, you could also try character classes that are describes by Unicode character properties like \p{L} for all Unicode letters.
Try preg_match(). http://php.net/manual/en/function.preg-match.php
On my registration page I need to validate the usernames as alphanumeric only, but also with optional underscores. I've come up with this:
function validate_alphanumeric_underscore($str)
{
return preg_match('/^\w+$/',$str);
}
Which seems to work okay, but I'm not a regex expert! Does anyone spot any problem?
The actual matched characters of \w depend on the locale that is being used:
A "word" character is any letter or digit or the underscore character, that is, any character which can be part of a Perl "word". The definition of letters and digits is controlled by PCRE's character tables, and may vary if locale-specific matching is taking place. For example, in the "fr" (French) locale, some character codes greater than 128 are used for accented letters, and these are matched by \w.
So you should better explicitly specify what characters you want to allow:
/^[A-Za-z0-9_]+$/
This allows just alphanumeric characters and the underscore.
And if you want to allow underscore only as concatenation character and want to force that the username must start with a alphabet character:
/^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/
Here's a custom function to validate the string by using the PHP ctype_alnum in conjunction with an array of allowed chars:
<?php
$str = "";
function validate_username($str) {
// each array entry is an special char allowed
// besides the ones from ctype_alnum
$allowed = array(".", "-", "_");
if ( ctype_alnum( str_replace($allowed, '', $str ) ) ) {
return $str;
} else {
$str = "Invalid Username";
return $str;
}
}
?>
try
function validate_alphanumeric_underscore($str)
{
return preg_match('/^[a-zA-Z0-9_]+$/',$str);
}
Looks fine to me. Note that you make no requirement for the placement of the underscore, so "username_" and "___username" would both pass.
I would take gumbo's secondary regex, to only allow underscore as concatenation, but add a + after the _ so a user can be like "special__username", just a minor tweak.
/^[A-Za-z][A-Za-z0-9]*(?:_+[A-Za-z0-9]+)*$/
Your own solution is perfectly fine.
preg_match uses Perl-like regular expressions, in which the character class \w defined to match exactly what you need:
\w - Match a "word" character (alphanumeric plus "_")
(source)