2016-07-26 203 views
0

我在解析從服務器獲取的消息時出現問題。這是消息:正則表達式來查找括號和引號之間的字符串

{"":["Current Password is invalid."]} 

我想要做的是顯示當前密碼在吐司無效,用正則表達式解析它。到目前爲止,我得到了:

^(\\:\\[\\\"(.*?)\\\"\\]? 

但它並沒有真正的工作。任何建議如何修復正則表達式?請記住,第一個引號(「:」之前)可能不總是空的。

+0

您輸入的樣子的Json所以它可能是最好使用一個JSON解析器。 – Thomas

+0

我看到你正在用'''量詞與']'。也許,所有你需要的是:「\\ [\」([^ \「] +)」'並使用'matcher.find()'訪問'group(1)'值。請參閱http://ideone.com/9M5W0r。 –

+0

@WiktorStribiżew我試過了,但它返回了一個空字符串。 –

回答

0

replaceAll()將爲您工作。

public static void main(String[] args) { 
    String s = "{\"\":[\"Current Password is invalid.\"]}"; 
    System.out.println(s.replaceAll(".*?\\[\"(.*?)\"\\].*", "$1")); 
    // ^^ Extract text between "[" and "] and replace the entire string with it. 
} 

O/P:

Current Password is invalid. 
+0

完美的作品!謝謝。 –