Leo*_*Leo 7 php java regex preg-match
在PHP中,如果我们需要匹配类似的东西["one","two","three"],我们可以使用以下正则表达式preg_match.
$pattern = "/\[\"(\w+)\",\"(\w+)\",\"(\w+)\"\]/"
Run Code Online (Sandbox Code Playgroud)
通过使用括号,我们还可以提取单词一,二和三.我知道MatcherJava 中的对象,但无法获得类似的功能; 我只能提取整个字符串.我将如何模仿preg_matchJava中的行为?
Col*_*ert 13
使用Matcher,要获得组,您必须使用该Matcher.group()方法.
例如 :
Pattern p = Pattern.compile("\\[\"(\\w+)\",\"(\\w+)\",\"(\\w+)\"\\]");
Matcher m = p.matcher("[\"one\",\"two\",\"three\"]");
boolean b = m.matches();
System.out.println(m.group(1)); //prints one
Run Code Online (Sandbox Code Playgroud)
记住group(0)是相同的整个匹配序列.
资源: