我有一个字符串如下:
“http:172.1。” = (10, 1,3);
“http:192.168。” = (15, 2,6);
"http:192.168.1.100" = (1, 2,8);
“ ”内的字符串是一个标签,() 内的字符串是前面标签的值。
返回我的正则表达式是什么:
标签: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 教程 - 正则表达式 http://java.sun.com/docs/books/tutorial/essential/regex/.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)