Q: java find() always returning true I am trying to find a pattern in the string in java. Below is the code written as-
 String line = "10011011001;0110,1001,1001,0,10,11";

 String regex ="[A-Za-z]?"; //[A-Za-z2-9\W]?
 //create a pattern obj
 Pattern p = Pattern.compile(regex);
 Matcher m = p.matcher(line);
 boolean a = m.find();
 System.out.println("The value of a is::"+a +" asdsd "+m.group(0));

I am expecting the boolean value to be false, but instead it is always returning as true. Any input or idea where I am going wrong.?

A: [A-Za-z]?  means "zero or one letters".  It will always match somewhere in the string; even if there aren't any letters, it will match zero of them.

A: The ? makes the entire character group optional. So your regex essentially means "find any character* ... or not". And the "or not" part means it matches the empty string.
* not really "any", just those characters that are represented in ASCII.

A:  The below regex should work;
[A-Za-z]?-----> once or not at all
 Reference :
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html


    String line = "10011011001;0110,1001,1001,0,10,11";

     String regex ="[A-Za-z]";// to find letter
     String regex ="[A-Za-z]+$";// to find last string..
     String regex ="[^0-9,;]";//means non digits and , ;


     //create a pattern obj
     Pattern p = Pattern.compile(regex);
     Matcher m = p.matcher(line);
     boolean a = m.find();
     System.out.println("The value of a is::"+a +" asdsd "+m.group(0));

