Java get matches group of Regex

前端 未结 2 539
余生分开走
余生分开走 2021-01-27 19:41

Given following Java expression codes:

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

if the match is true, it

2条回答
  •  醉话见心
    2021-01-27 20:20

    To access the groups you need to use Matcher: Pattern.compile(regex).matcher(row).

    Then you can call find() or matches() on the matcher to execute the matcher and if they return true you can access the groups via group(1) and 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 );
    }
    

提交回复
热议问题