什么是正则表达式,用于查找“”之间的字符串

我有一个字符串如下:

“HTTP:172.1。” =(10,1,3);

“HTTP:192.168。” =(15,2,6);

“http:192.168.1.100”=(1,2,8);

“”里面的字符串是Tag,而inside()是前面标记的值。 什么是将返回我的正则表达式:Tag:http:172.1。 价值:10,1,3

这个正则表达式

"([^\"]*)"\s*=\s*\(([^\)]*)\)*. 

返回引号“”作为组1,而括号()中的文本作为组2返回。

注意:将其保存为字符串时,您必须转义引号字符并加倍斜杠。 它很快变得难以理解 – 像这样:

 "\"([^\\\"]*)\"\\s*=\\s*\\(([^\\)]*)\\)*." 

编辑:根据要求,这是一个使用示例:

  Pattern p = Pattern.compile("\"([^\\\"]*)\"\\s*=\\s*\\(([^\\)]*)\\)*."); // put p as a class member so it's computed only once... String stringToMatch = "\"http://123.45\" = (0,1,3)"; // the string to match - hardcoded here, but you will probably read // this from a file or similar Matcher m = p.matches(stringToMatch); if (m.matches()) { String url = p.group(1); // what's between quotes String value = p.group(2); // what's between parentheses System.out.println("url: "+url); // http://123.45 System.out.println("value: "+value); // 0,1,3 } 

有关更多详细信息,请参阅Sun Tutorial – 正则表达式 。