import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class test_regex {
public static void main(String[] args)
{
String text = "(asdf) (123) (zxcv)";
Pattern p = Pattern.compile("^\\((\\S+)\\) \\((\\S+)\\) \\((\\S+)\\)$");
Matcher m = p.matcher(text);
if (m.matches())
for (int i=0; i<m.groupCount(); i++)
System.out.println(i+".) "+m.group(i));
else
System.out.println("no match");
}
}
Output:
0.) (asdf) (123) (zxcv)
1.) asdf
2.) 123
I want my pattern to output:
0.) asdf
1.) 123
2.) zxcv
It seems like my pattern is taking the first ( and last ) to match the first pattern to the entire text. Is there any way to limit this type of regex to do as I want it to?