开发者社区 问答 正文

一个关于正则表达式的问题

{"resultcode":910005,"resultmsg":[api接口]无法查询到应用记录,"transactionid":""}
如何让上面字符串的 resultmsg值 [api接口]无法查询到应用记录 加上双引号 像这样
screenshot
不知道用正则表达式能不能弄出来?

展开
收起
蛮大人123 2016-06-07 14:51:45 1796 分享 版权
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    正则表达式:
    (.*?resultmsg\":)(.*?)(,.*?})
    正则用小括号分成三组,group(1)则代表匹配出的结果的第一部分的内容。依此类推。
    java程序代码:

     /*
    * @author   fateflv
    * @version  1.0
    * @since    2016-03-26
    * @site     http://my.csdn.net/fateflv
    */
    import java.util.regex.*;
    public class reg {
        public static void main(String[]agrs)
        {
    
            Pattern p=Pattern.compile("(.*?resultmsg\":)(.*?)(,.*?})"); 
    
            //String s = "{\"resultcode\":910005,\"resultmsg\":[api接口]无法查询到应用记录,\"transactionid\":\"\"}";
    
            Matcher m=p.matcher("{\"resultcode\":910005,\"resultmsg\":[api接口]无法查询到应用记录,\"transactionid\":\"\"}");
    
            while(m.find()) { 
                System.out.println("要匹配的原串为:\n"+m.group(0));
                System.out.println(); 
                System.out.println("第1组: "+m.group(1));
                System.out.println("第2组: "+m.group(2));
                System.out.println("第3组: "+m.group(3)); 
                System.out.println("处理后: \n"+m.group(1)+"\""+m.group(2)+"\""+m.group(3)); 
            }
        }
    }
    2019-07-17 19:30:11
    赞同 展开评论
问答分类:
API
问答地址: