Reputation: 27392
How can you determine if a string is all caps with a regular expression. It can include punctuation and numbers, just no lower case letters.
Upvotes: 32
Views: 33475
Reputation: 523534
m/^[^a-z]*$/
For non-English characters,
m/^\P{Ll}*$/
(\P{Ll}
is the same as [^\p{Ll}]
, which accepts all characters except the ones marked as lower-case.)
Upvotes: 29
Reputation: 3020
Basic:
^[^a-z]*$
Exclude empty lines:
^[^a-z]+$
Catering for exclusion of non-english chars (e.g. exclude a string containing à like 'VOILà'):
^\P{Ll}*$
Expressions (e.g. JS):
Single line:
/^[^a-z]*$/
Multi line:
/^[^a-z]*$/m
Theory:
[a-z]
matches a character that is a,b,c,...z
[a-z]*
matches a series of characters that are a,b,c,...z
^[a-z]*
matches a series of characters that are not a,b,c...z
^[^a-z]*$
matches a string only containing a series of characters that are not a,b,c,...z from beginning to end.
^[^a-z]+$
ensures that there is at least one character that is not a,b,c,...z, and that the remaining characters are not a,b,c,...z in the string from beginning to end.
^\P{Ll}*$
matches all Unicode characters in the 'letter' group that have an uppercase variant - see https://www.regular-expressions.info/.
Upvotes: 1
Reputation: 59461
If you want to match the string against another regex after making sure that there are no lower case letters, you can use positive lookahead.
^(?=[^a-z]*$)MORE_REGEX$
For example, to make sure that first and last characters are alpha-numeric:
^(?=[^a-z]*$)[A-Z0-9].*[A-Z0-9]$
Upvotes: 2
Reputation: 342679
$str="ABCcDEF";
if ( preg_match ("/[a-z]/",$str ) ){
echo "Lowercase found\n";
}
Upvotes: 0
Reputation: 77074
The string contains a lowercase letter if the expression /[a-z]/
returns true, so simply perform this check, if it's false you have no lowercase letters.
Upvotes: 1
Reputation: 7405
Why not just use if(string.toUpperCase() == string)? ._. Its more "elegant"...
I think you're trying to force in RegExp, but as someone else stated, I don't think this is the best use of regexp...
Upvotes: 5