2016-07-26 202 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

完美的作品!谢谢。 –