java 正则表达式替换字符
我想把"requirement":"lt","threshold_value":"300","alert_num":"1","unit":"s"替换成"requireme...
我想把"requirement":"lt","threshold_value":"300","alert_num":"1","unit":"s"
替换成"requirement":"lt","threshold_value":["300"],"alert_num":"1","unit":"s"
也有这种字符串"requirement":"lt","threshold_value":"300","200","alert_num":"1","unit":"s"
需要替换成"requirement":"lt","threshold_value":["300","200"],"alert_num":"1","unit":"s"
现在我代码是这样:
String srcString = "\"requirement\":\"lt\",\"threshold_value\":\"300\",\"alert_num\":\"1\",\"unit\":\"s\"";
System.out.println(srcString);
//String s = "11111111111111x\"123\"x111111111";
srcString= srcString.replaceAll("threshold_value\":\"*\"", "threshold_value\":[\"");
System.out.println(srcString);
替换出来是这样的"requirement":"lt","threshold_value":["300","alert_num":"1","unit":"s"
求解。。。要用replaceAll正则。。。。 展开
替换成"requirement":"lt","threshold_value":["300"],"alert_num":"1","unit":"s"
也有这种字符串"requirement":"lt","threshold_value":"300","200","alert_num":"1","unit":"s"
需要替换成"requirement":"lt","threshold_value":["300","200"],"alert_num":"1","unit":"s"
现在我代码是这样:
String srcString = "\"requirement\":\"lt\",\"threshold_value\":\"300\",\"alert_num\":\"1\",\"unit\":\"s\"";
System.out.println(srcString);
//String s = "11111111111111x\"123\"x111111111";
srcString= srcString.replaceAll("threshold_value\":\"*\"", "threshold_value\":[\"");
System.out.println(srcString);
替换出来是这样的"requirement":"lt","threshold_value":["300","alert_num":"1","unit":"s"
求解。。。要用replaceAll正则。。。。 展开
展开全部
(\"threshold_value\":)((?:,?\"\\d+\")+)
替换成
\\1[\\2]
替换成
\\1[\\2]
追问
结果变成这样了"requirement":"lt",1[2],"alert_num":"1","unit":"s"
应该变成这样:"requirement":"lt","threshold_value":["300"],"alert_num":"1","unit":"s"
追答
srcString.replaceAll("(\"threshold_value\":)((?:,?\"\\d+\")+)", "$1[$2]");
展开全部
package zhidao;
public class Ex4_9
{
public static void main ( String args[] )
{
String str1 = "\"requirement\":\"lt\",\"threshold_value\":\"300\",\"alert_num\":\"1\",\"unit\":\"s\"";
String reg = "(\\:)((\"\\d+\",?)+)(,.*)";
System.out.println (str1.replaceAll (reg, "$1[$2]$4"));
String str2 = "\"requirement\":\"lt\",\"threshold_value\":\"300\",\"200\",\"alert_num\":\"1\",\"unit\":\"s\"";
System.out.println (str2.replaceAll (reg, "$1[$2]$4"));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询