Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if string is all caps with regular expression

Tags:

regex

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.

like image 860
Brian Avatar asked Feb 24 '10 05:02

Brian


People also ask

How do you check if a string is uppercase?

To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase. Copied!

How do I find the capital letter of a regular expression?

For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.


3 Answers

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.)

like image 137
kennytm Avatar answered Oct 16 '22 14:10

kennytm


That sounds like you want: ^[^a-z]*$

like image 10
Jerry Coffin Avatar answered Oct 16 '22 14:10

Jerry Coffin


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...

like image 4
Warty Avatar answered Oct 16 '22 15:10

Warty