Java获得正则表达式的匹配组

给出以下Java表达式代码:

boolean match = row.matches("('.*')?,('.*')?"); 

如果matchtrue ,则表示正则表达式匹配整个“行”。 然后我可以获得两组的内容吗? 每一个都是('.*')

要访问组,您需要使用MatcherPattern.compile(regex).matcher(row)

然后,您可以在匹配器上调用find()matches()来执行匹配器,如果它们返回true,则可以通过group(1)group(2)访问组。

 String row = "'what','ever'"; Matcher matcher = Pattern.compile("('.*')?,('.*')?").matcher( row ); if( matcher.matches() ) { String group1 = matcher.group( 1 ); String group2 = matcher.group( 2 ); } 

你可以尝试String[] groups = row.split(","); 。 然后,您可以使用groups[0]groups[1]来获取您正在寻找的每个“组”。