Q:

Regular expression is not working with single character

I am trying to write regular expression in Java to evaluate two strings mentioned with () separated by , 
Example: (test1,test2)

I have written below code
public static void main(String[] a){
    String pattern = "\\([a-zA-Z0-9]+,[a-zA-Z0-9]+.\\)";
    String test = "(test1,test2)";
    System.out.println(test.matches(pattern));
}

It works as expected and prints true in below cases 
String test = "(test1,test2)";

String test = "(t,test2)";

But it is printing false when I send below 
String test = "(test1,t)";

It is strange because I am using same expression before and after ,
It returns true for (t,test2) but not for (test1,t)
Please let me know what am I missing in this regular expression. I need it to evaluate and return true for (test1,t) 

A:

There's no need for the . (that matches one character) in your regex. Remove . from your regex so it becomes "\\([a-zA-Z0-9]+,[a-zA-Z0-9]+\\)" and it should work.

A:

Use this regex:
String pattern = "^\\(.+,.+\\)";

This will match your required strings.

