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.
如何确定字符串是否都是带正则表达式的大写字母。它可以包括标点符号和数字,只是没有小写字母。
8 个解决方案
#1
19
m/^[^a-z]*$/
For non-English characters,
对于非英文字符,
m/^[^\p{Ll}]*$/
#2
8
That sounds like you want: ^[^a-z]*$
这听起来像你想要的:^ [^ a-z] * $
#3
5
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...
为什么不使用if(string.toUpperCase()== string)? ._。它更“优雅”......我认为你试图强迫RegExp,但正如其他人所说的那样,我不认为这是正则表达式的最佳用途......
#4
1
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.
如果表达式/ [a-z] /返回true,则字符串包含小写字母,因此只需执行此检查,如果它为false,则表示没有小写字母。
#5
1
Simplest would seem to be:
最简单的似乎是:
^[^a-z]*$
#6
0
How about (s == uppercase(s))
--> string is all caps?
怎么样(s == uppercase(s)) - >字符串是全部大写?
#7
0
$str="ABCcDEF";
if ( preg_match ("/[a-z]/",$str ) ){
echo "Lowercase found\n";
}
#8
0
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]$
#1
19
m/^[^a-z]*$/
For non-English characters,
对于非英文字符,
m/^[^\p{Ll}]*$/
#2
8
That sounds like you want: ^[^a-z]*$
这听起来像你想要的:^ [^ a-z] * $
#3
5
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...
为什么不使用if(string.toUpperCase()== string)? ._。它更“优雅”......我认为你试图强迫RegExp,但正如其他人所说的那样,我不认为这是正则表达式的最佳用途......
#4
1
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.
如果表达式/ [a-z] /返回true,则字符串包含小写字母,因此只需执行此检查,如果它为false,则表示没有小写字母。
#5
1
Simplest would seem to be:
最简单的似乎是:
^[^a-z]*$
#6
0
How about (s == uppercase(s))
--> string is all caps?
怎么样(s == uppercase(s)) - >字符串是全部大写?
#7
0
$str="ABCcDEF";
if ( preg_match ("/[a-z]/",$str ) ){
echo "Lowercase found\n";
}
#8
0
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]$