There is no "match as many time as possible and join all the groups together" in Java.
You either have to do it yourself using:
while(matcher.find()) {
// ...
}
... or using a regex that already matches everything in a single call to find
.
For example, you could try the following regex (as Java String) instead, which will match all your attributes at once.
(?:\\w+ (?:INT|CHAR(?:\\(\\d{1,3}\\))?|DEC)(?:, )?)+
Here is a working example:
final String str = "CREATE TABLE student (id INT, name CHAR(20), gpa DEC);";
final Pattern p = Pattern.compile("(?:\\w+ (?:INT|CHAR(?:\\(\\d{1,3}\\))?|DEC)(?:, )?)+");
final Matcher m = p.matcher(str);
if(m.find()) {
System.out.println(m.group()); // prints "id INT, name CHAR(20), gpa DEC"
};
Output:
id INT, name CHAR(20), gpa DEC