天天看點

正規表達式執行個體

1,擷取引号中的json字元串

正規表達式執行個體

@test  

    public void test_json(){  

        string input="\"normalprice\": \"{\"storageprice\":66,\"ud1price\":1,\"userperiodprice\":99}\",";  

        system.out.println(input);  

        string regex=".*\"\\{([^{}]*)\\}\".*";  

        string result=input.replaceall(regex, "$1");  

        system.out.println(result);  

    }  

正規表達式執行個體

2, 遇到的問題:

正規表達式執行個體

解決方法:

正規表達式執行個體

/*** 

     * ("normalprice": "{"storageprice":66,"ud1price":1,"userperiodprice":99}",) 

     * to<br> 

     * ("normalprice": {"storageprice":66,"ud1price":1,"userperiodprice":99},) 

     * @param input 

     * @param isstrict : 是否嚴格,true:[^{}]<br> 

     * false:.* 

     * @return 

     */  

    public static string getjsonfromquotes(string input,boolean isstrict){  

        string regexloose="\"(\\{.*\\})\"";  

        string regexstrict="\"(\\{[^{}]*\\})\"";  

        string regex=null;  

        if(isstrict){  

            regex=regexstrict;  

        }else{  

            regex=regexloose;  

        }  

        return result;  

參數說明:

isstrict:

(a)true:嚴格模式,"{和}" 之間不能包含{或}

(b)false:非嚴格模式:"{和}"之間可以包含任意字元

應用:

正規表達式執行個體

if (command.equals("unquotesjson")) {  

            string text = this.ta.gettext();  

            if(text!=null){  

                text=regexutil.getjsonfromquotes(text, false);  

                this.ta.settext(text);  

            }