我有像这样的字符串
"{"name:John","Age:15","width:500","height:300"},{"name:Mike","Age:25","width:600","height:400"}";
我如何在宽度和高度之后替换数字,就像这样
"{"name:John","Age:15","width:100","height:100"},{"name:Mike","Age:25","width:100","height:100"}";
由于
答案 0 :(得分:0)
使用此正则表达式进行搜索:
(width|height):\d+
并替换为:
$1:100
<强>爪哇:强>
str = str.replaceAll("(width|height):\\d+", "$1:100");
答案 1 :(得分:0)
在Java中,我们可以在后视(see Java takes things a step further by allowing finite repetition)中使用备选列表,它们的长度不必相同:
String str = "\"{\"name:John\",\"Age:15\",\"width:500\",\"height:300\"},{\"name:Mike\",\"Age:25\",\"width:600\",\"height:400\"}\";";
System.out.println(str.replaceAll("(?i)(?<=width:|height:)\\d+", "100"));
输出demo program:
"{"name:John","Age:15","width:100","height:100"},{"name:Mike","Age:25","width:100","height:100"}";