1

Javaで文字列のパターンを見つけようとしています。以下は、次のように記述されたコードです-

 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));

ブール値が false であることを期待していますが、代わりに常に true が返されます。私が間違っているところに何か入力やアイデアがありますか?

4

3 に答える 3

1
 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));
于 2013-05-27T18:58:16.993 に答える