我需要不受字符限制的字符串的java模式。
字符串的正則表達式模式不受字符限制
我有一個字符串(如下所述),一些花括號括在單引號和其他大括號內。我想用另一個字符串替換不受單引號限制的大括號。
原始字符串:
this is single-quoted curly '{'something'}' and this is {not} end
需要轉換到
this is single-quoted curly '{'something'}' and this is <<not>> end
注意,大括號{}不是由單引號包圍已被替換< < >>。
然而,我的代碼打印(字符被吃掉)的文本
this is single-quoted curly '{'something'}' and this is<<no>> end
當我使用模式
[^']([{}])
我的代碼是
String regex = "[^']([{}])";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
if ("{".equals(matcher.group(1))) {
matcher.appendReplacement(strBuffer, "<<");
} else if ("}".equals(matcher.group(1))) {
matcher.appendReplacement(strBuffer, ">>");
}
}
matcher.appendTail(strBuffer);
+1,感謝您指出我的(現已刪除)答案中的缺陷。 – jlordo
非常感謝,這很有幫助 –
對不起,大家,無法對答案投票(說...沒有足夠的聲譽)。對「這篇文章對你有用嗎?」回答「是」。題。 –