Java 字符串中大写字母的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20661724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular Expression for UpperCase Letters In A String
提问by David
For the life of me, I can't figure out why this regular expression is not working. It should find upper case letters in the given string and give me the count. Any ideas are welcome.
对于我的生活,我无法弄清楚为什么这个正则表达式不起作用。它应该在给定的字符串中找到大写字母并给我计数。欢迎任何想法。
Here is the unit test code:
下面是单元测试代码:
public class RegEx {
@Test
public void testCountTheNumberOfUpperCaseCharacters() {
String testStr = "abcdefghijkTYYtyyQ";
String regEx = "^[A-Z]+$";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(testStr);
System.out.printf("Found %d, of capital letters in %s%n", matcher.groupCount(), testStr);
}
}
采纳答案by Marko Topolnik
You didn't call
matches
orfind
on the matcher. It hasn't done any work.getGroupCount
is the wrong method to call. Your regex has no capture groups, and even if it did, it wouldn't give you the character count.
你没有打电话
matches
或find
在匹配器上。它没有做任何工作。getGroupCount
是错误的调用方法。您的正则表达式没有捕获组,即使有,也不会给您字符数。
You should be using find
, but with a different regex, one without anchors. I would also advise using the proper Unicode character class: "\\p{Lu}+"
. Use this in a while (m.find())
loop, and accumulate the total number of characters obtained from m.group(0).length()
at each step.
您应该使用find
,但使用不同的正则表达式,一个没有锚点的正则表达式。我还建议使用正确的 Unicode 字符类:"\\p{Lu}+"
. 在while (m.find())
循环中使用它,并累积从m.group(0).length()
每一步获得的字符总数。
回答by anubhava
It doesn't work because you have 2 problems:
它不起作用,因为您有两个问题:
- Regex is incorrect, it should be
"[A-Z]"
for ASCII letter or\p{Lu}
for Unicode uppercase letters - You're not calling
while (matcher.find())
beforematcher.groupCount()
- 正则表达式不正确,应该是
"[A-Z]"
ASCII 字母或\p{Lu}
Unicode 大写字母 - 你
while (matcher.find())
之前没有打电话matcher.groupCount()
Correct code:
正确代码:
public void testCountTheNumberOfUpperCaseCharacters() {
String testStr = "abcdefghijkTYYtyyQ";
String regEx = "(\p{Lu})";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(testStr);
while (matcher.find())
System.out.printf("Found %d, of capital letters in %s%n",
matcher.groupCount(), testStr);
}
UPDATE: Use this much simpler one-liner codeto count number of Unicode upper case letters in a string:
更新:使用这个更简单的单行代码来计算字符串中 Unicode 大写字母的数量:
int countuc = testStr.split("(?=\p{Lu})").length - 1;
回答by dasblinkenlight
It should find upper case letters in the given string and give me the count.
它应该在给定的字符串中找到大写字母并给我计数。
No, it shouldn't: the ^
and $
anchors prevent it from doing so, forcing to look for a non-empty string composed entirelyof uppercase characters.
不,它不应该:^
和$
锚点阻止它这样做,强制查找完全由大写字符组成的非空字符串。
Moreover, you cannot expect a group count in an expression that does not define groups to be anything other than zero (no matches) or one (a single match).
此外,您不能期望未将组定义为 0(无匹配)或 1(单个匹配)以外的任何值的表达式中的组计数。
If you insist on using a regex, use a simple [A-Z]
expression with no anchors, and call matcher.find()
in a loop. A better approach, however, would be calling Character.isUpperCase
on the characters of your string, and counting the hits:
如果您坚持使用正则表达式,请使用[A-Z]
没有锚点的简单表达式,并matcher.find()
在循环中调用。然而,更好的方法是调用Character.isUpperCase
字符串的字符,并计算命中数:
int count = 0;
for (char c : str.toCharArray()) {
if (Character.isUpperCase(c)) {
count++;
}
}
回答by James Gawron
Your pattern as you've written it looks for 1 or more capital letters between the beginning and the end of the line...if there are any lowercase characters in the line it won't match.
您编写的模式会在行的开头和结尾之间查找 1 个或多个大写字母……如果该行中有任何小写字符,它将不匹配。
回答by M21B8
This should do what you're after,
这应该做你所追求的,
@Test
public void testCountTheNumberOfUpperCaseCharacters() {
String testStr = "abcdefghijkTYYtyyQ";
String regEx = "[A-Z]+";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(testStr);
int count = 0;
while (matcher.find()) {
count+=matcher.group(0).length();
}
System.out.printf("Found %d, of capital letters in %s%n", count, testStr);
}
回答by ganesh konathala
In this example i'm using a regex(regular Expression) to count the number of UpperCase and LowerCase letters in the given string using Java.
在这个例子中,我使用正则表达式(正则表达式)来计算使用 Java 的给定字符串中大写和小写字母的数量。
import java.util.regex.*;
import java.util.Scanner;
import java.io.*;
public class CandidateCode {
public static void main(String args[] ) throws Exception {
Scanner sc= new Scanner(System.in);
// Reads the String of data entered in a line
String str = sc.nextLine();
//counts uppercase letteres in the given String
int countuc = str.split("([A-Z]+?)").length;
//counts lowercase letteres in the given String
int countlc = str.split("([a-z]+?)").length;
System.out.println("UpperCase count: "+countuc-1);
System.out.println("LowerCase count: "+countlc-1);
}
}
回答by vivekkurien
Change the regular expression to [A-Z]which checks all occurrences of capital letters
将正则表达式更改为 [AZ]以检查所有出现的大写字母
Please refer the below example which counts number of capital letters in a string using pattern
请参考以下示例,该示例使用模式计算字符串中的大写字母数
@Test
public void testCountTheNumberOfUpperCaseCharacters() {
Pattern ptrn = Pattern.compile("[A-Z]");
Matcher matcher = ptrn.matcher("ivekKVVV");
int from = 0;
int count = 0;
while(matcher.find(from)) {
count++;
from = matcher.start() + 1;
}
System.out.println(count);
}
}
}