【发布时间】:2013-01-16 08:33:21
【问题描述】:
我需要不以字符为界的字符串的 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);
【问题讨论】: