文字で区切られていない文字列の 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);