从字符串[foo](bar)
我想提取foo
和bar
部分:帮助,正则表达式
Pattern p = Pattern.compile("\\[(.*)\\]\\((.*)\\)");
String input = "[foo](bar)";
assert p.matcher(input).matches();
String[] a = ??? // should be initialized to {"foo", "bar"}
我怎么在第四行写获得来自输入foo
和bar
?
您应该使用p.matcher(输入),返回你[匹配](http://download.oracle。 com/javase/7/docs/api/java/util/regex/Matcher.html)对象,其中包含您的匹配项。然后,您可以使用find()来查找字符串中的下一个字符串,并使用group()来访问最后找到的字符串。 – 2011-05-31 18:58:18