我有一个包含 xyaahhfhajfahj{adhadh}fsfhgs{sfsf}
的字符串。
现在我想用空格替换{string}
。
我想用空替换大括号和其中的字符串。
我想为它使用 replaceFirst
但我不知道执行此操作的正则表达式。
最佳答案
试试这个:
public class TestCls {
public static void main(String[] args) {
String str = "xyaahhfhajfahj{adhadh}fsfhgs{sfsf}";
String str1 = str.replaceAll("\\{[a-zA-z0-9]*\\}", " ");// to replace string within "{" & "}" with " ".
String str2 = str.replaceFirst("\\{[a-zA-z0-9]*\\}", " ");// to replace first string within "{" & "}" with " ".
System.out.println(str1);
System.out.println(str2);
}
}
关于java - 如何使用 replaceFirst 替换 {....},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5658794/