我有一个像这样的字符串:
String a = "Barbara Liskov (born Barbara Jane Huberman on November 7, 1939"
+" in California) is a computer scientist.[2] She is currently the Ford"
+" Professor of Engineering in the MIT School of Engineering's electrical"
+" engineering and computer science department and an institute professor"
+" at the Massachusetts Institute of Technology.[3]";
我想用以下元素替换所有这些元素:[1]
、[2]
、[3]
等空格。
我尝试过:
if (a.matches("([){1}\\d(]){1}")) {
a = a.replace("");
}
但是它不起作用!
最佳答案
你的模式
完全错误。
试试这个例子:
String input =
"Barbara Liskov (born Barbara Jane Huberman on November 7, 1939 in California) "
+ "is a computer scientist.[2] She is currently the Ford Professor of Engineering "
+ "in the MIT School of Engineering's electrical engineering and computer "
+ "science department and an institute professor at the Massachusetts Institute "
+ "of Technology.[3]";
// | escaped opening square bracket
// | | any digit
// | | | escaped closing square bracket
// | | | | replace with one space
System.out.println(input.replaceAll("\\[\\d+\\]", " "));
输出(为了清晰起见添加了换行符)
Barbara Liskov (born Barbara Jane Huberman on November 7,
1939 in California) is a computer scientist.
She is currently the Ford Professor of Engineering in the MIT
School of Engineering's electrical engineering and computer science
department and an institute professor at the Massachusetts Institute of Technology.
关于java - Java中的字符串匹配和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24383939/