I have the following regular expression (link)
我有以下正则表达式(链接)
[\d\.]+[ ](.*?)[ ]{2,}(.+)
However the equivalent Java code, fails to match:
但是,等效的Java代码无法匹配:
String REGEX = "[\\d\\.]+[ ](.*?)[ ]{2,}(.+)";
Pattern pattern = Pattern.compile(REGEX);
String line = "1. QUEEN WE ARE THE CHAMPIONS"
Matcher matcher= pattern.matcher(line);
String artist = matcher.group(0);
String song = matcher.group(1);
I can't seem to understand what goes wrong, any ideas?
我似乎无法理解出了什么问题,任何想法?
2 个解决方案
#1
5
You need to call find
to move to the first match. Add matcher.find();
before calling group()
.
你需要调用find来移动到第一场比赛。添加matcher.find();在调用group()之前。
Once you do that your code works as expected.
完成后,您的代码将按预期工作。
#2
1
You need to call matcher.matches() before group.
你需要在group之前调用matcher.matches()。
if(matcher.matches()){
String artist = matcher.group(0);
System.out.println(artist);
String song = matcher.group(1);
System.out.println(song);
}
#1
5
You need to call find
to move to the first match. Add matcher.find();
before calling group()
.
你需要调用find来移动到第一场比赛。添加matcher.find();在调用group()之前。
Once you do that your code works as expected.
完成后,您的代码将按预期工作。
#2
1
You need to call matcher.matches() before group.
你需要在group之前调用matcher.matches()。
if(matcher.matches()){
String artist = matcher.group(0);
System.out.println(artist);
String song = matcher.group(1);
System.out.println(song);
}