導入Java . util . regex . pattern;
公共類StringReplace {
public static final String regex = " \ \ {((?:\ \ w+))\ \ } ";
公共靜態最終字符串輸入= "{}...[A]B...[1{1}2]...{}";
公共靜態最終字符串替換= " & lt$1 >;
公共靜態void main(String[] args) {
模式p = pattern . compile(regex);
Matcher m = p.matcher(輸入);
string buffer buffer = new string buffer();
while (m.find()) {
m.appendReplacement(緩沖區,替換);
}
m.appendTail(緩沖區);
字符串result = buffer . tostring();
System.out.println(結果);
//結果是{}...[a & lt;A & gtb & ltB& gt;c]...[1 & lt;1 & gt;2]...{}
}
}