0

Given following Java expression codes:

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

if the match is true, it means the regex matches whole 'row'. Then I could I get the content of the two groups? Each one is ('.*')?

2 Answers 2

3

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 ); } 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.