我尝试运行这个测试开始:线移动到一个字符串
@Test
public void moveLastUpdateToFirstLine() throws Exception {
StringUtils stringUtils = new StringUtils();
String newString = stringUtils.moveLastUpdateToFirstLine("bbbbbbb last_update : 188908098 cccc");
assert (newString.equals("last_update : 188908098 bbbbbbb cccc"));
}
public String moveLastUpdateToFirstLine(String venueConfigurationStr) {
Pattern pattern = Pattern.compile("(last_update : \\d+)");
Matcher matcher = pattern.matcher(venueConfigurationStr);
if (matcher.find()) {
venueConfigurationStr = matcher.group(1) + venueConfigurationStr.replace(matcher.group(1), "");
}
return venueConfigurationStr;
}
但它说:
java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:536)
如何解决我的匹配?
你可以检查matcher.matches()而不是matcher.find()吗? – Techidiot
你的'Pattern'期待匹配整行。用'。*'包围它或者适当地设置你的选项。 https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html –
当我测试代码时,它工作正常(虽然它缺少'newString'中的空间)。 – resueman