【发布时间】:2017-06-12 03:00:39
【问题描述】:
我想知道为什么这个正则表达式组在 JAVA 中不适合我?查找组匹配时抛出异常。我正在尝试匹配由破折号分隔的数字。
Pattern p = Pattern.compile("([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)");
Matcher matcher = p.matcher("1-1-3-1-4");
matcher.group(0); // Exception happens here - java.lang.IllegalStateException: No match found
【问题讨论】:
-
没有。你能否指出,如果有什么具体的原因导致这个问题?
-
使用
matcher()实际上并没有尝试匹配任何东西。它只是设置了匹配器。您必须告诉它尝试与Matchermethods 之一匹配。 -
使用
matcher.find()遍历所有的事件。 -
这就是他使用
group做的事情 -
@f.khantsis Have a look at this