java模式从mapper生成的字符串中清除其他字符周围的引号

时间:2017-01-23 20:42:04

标签: java regex pattern-matching replaceall

我需要将json mapper生成的字符串序列替换为fallow:

:"{ -> :{   
}"} -> }} 

如何看待这种模式?

更新:完整字符串示例

{"method":"createInvoice","params":"{"btcDue":null,"btcPaid":null,
                        "btcPrice":null,"currency":"PLN","currentTime":null,
                        "exceptionStatus":null,"expirationTime":null,
                        "guid":"99250130","id":null,"invoiceTime":null,
                        "paymentUrls":null,"price":1.23,"rate":null,
                        "status":null,"transactions":null,"url":null
}"} 

但假设我们将有更多的实例替换为2 :)

澄清:android字符串方法

 public String replace(CharSequence target, CharSequence replacement) {
    String replacementStr = replacement.toString();
    String targetStr = target.toString();
    // Special case when target == "". 
    // .. cut 
    // This is the "regular" case.
    int lastMatch = 0;
    StringBuilder sb = null;
    for (;;) {
        int currentMatch = indexOf(this, targetStr, lastMatch);
        if (currentMatch == -1) {
            break;
        }
        if (sb == null) {
            sb = new StringBuilder(count);
        }
        sb.append(this, lastMatch, currentMatch);
        sb.append(replacementStr);
        lastMatch = currentMatch + targetStr.count;
    }

    if (sb != null) {
        sb.append(this, lastMatch, count);
        return sb.toString();
    } else {
        return this;
    }
 }

 public String replaceAll(String regex, String replacement) {
    return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}

3 个答案:

答案 0 :(得分:1)

没什么难的:

String result = "{\"method\":\"createInvoice\",\"params\":\"{\"btcDue\":null,\"btcPaid\":null,\"btcPrice\":null,\"currency\":\"PLN\",\"currentTime\":null,\"exceptionStatus\":null,\"expirationTime\":null,\"guid\":\"99250130\",\"id\":null,\"invoiceTime\":null,\"paymentUrls\":null,\"price\":1.23,\"rate\":null,\"status\":null,\"transactions\":null,\"url\":null}\"}"
    .replace(":\"{", ":{")
    .replace("}\"}", "}}");
System.out.println(result);

答案 1 :(得分:0)

您可以使用lookahead/lookbehind构造匹配(function() { var dialog = $('#your-dialog-selector'), // Your dialog select = $('#createNewSelect'); // The select box (get rid of the second id attribute [id="select"]!) // Listen for the form's submit event select.closest('form').on('submit', function(e) { e.preventDefault(); // Don't actually submit window.location = "/" + select.val(); // Navigate }); // To open the dialog.. put it wherever dialog.modal('show'); })(); + :{ + }所包围的所有引号:

}

将其传递给(?<=})"(?=})|(?<=:)"(?={) 以删除引号(demo)。

答案 2 :(得分:0)

搜索

def joinTerminateLeft (xs : List[String], term : String) : String = {
  def f(s: String)(s2: String, s3: String) : String = s2 + s + s3
  xs match {
    case Nil => ""
    case x::xs => x + foldLeft(xs, "", f(term))
  }
}

然后将其替换为&#34;&#34;

原始文字

\"

<强>结果

:"{   
}"}

JAVA CODE

:{
}}