[英]Java Regex Replacement Issue
I have the following code... 我有以下代码...
private static final String REGEX_TEAMSPLIT =
"([0-9]*)\\s-\\s([A-Z,a-z][a-z,\\s,A-Z]*)";
...
String reg = teamNames.get(0).html();
//reg == '62401 - Breakers'
teamNumber = reg.replace(REGEX_TEAMSPLIT, "$1");
//teamNumber == '62401 - Breakers'
Now I was assuming this would leave me with only the first group per what I read online. 现在我假设这将使我在网上阅读的内容只剩下第一组。 But as you can see this is not the case. 但正如您所看到的,情况并非如此。 Can someone tell what I am missing? 有人可以告诉我我所缺少的吗?
You should use replaceAll
instead of replace
. 您应该使用replaceAll
而不是replace
。 ( replace
method does not interpret the first parameter as regular expression, but matches literally) ( replace
方法不会将第一个参数解释为正则表达式,而是从字面上匹配)
String REGEX_TEAMSPLIT = "([0-9]*)\\s-\\s([A-Z,a-z][a-z,\\s,A-Z]*)";
String reg = "62401 - Breakers";
String teamNumber = reg.replaceAll(REGEX_TEAMSPLIT, "$1");
System.out.println(teamNumber); // => 62401
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.