Java get matches group of Regex

前端 未结 2 538
余生分开走
余生分开走 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:12

    You could try String[] groups = row.split(",");. Then you can use groups[0] and groups[1] to get each of the 'groups' you're looking for.

    0 讨论(0)
  • 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 );
    }
    
    0 讨论(0)
提交回复
热议问题