模式行不断抛出空指针,我不明白为什么。我尝试使用Pattern.quote
,但它出现了:
Pattern p: \Q.*(bread){1, 1}.{1, 1}(bread){1, 1}.*\E|java.util.regex.Matcher[pattern=\Q.*(bread){1, 1}.{1, 1}(bread){1, 1}.*\E region=0,13 lastmatch=]
这完全不是应该的样子。我想在两个bread
子串之间返回字符串。
public String getSandwich(String str) {
Pattern p = Pattern.compile(".*(bread){1, 1}(.{1, 1})(bread){1, 1}.*");
Matcher m = p.matcher(str);
if (m.find()) {
return m.group(2);
} else {
return "";
}
}
有什么想法吗?
答案 0 :(得分:0)
您的模式中存在语法错误:
.*(bread){1, 1}(.{1, 1})(bread){1, 1}.*
no space ---^ here -^ and here ---^
顺便说一下,量化器的表达方式是什么,如{1,1}
?