示例:
答案 0 :(得分:11)
public class HasCharacters {
public static void main( String [] args ){
if( args[0].matches(".*[a-zA-Z]+.*")){
System.out.println( "Has characters ");
} else {
System.out.println("Ok");
}
}
}
测试
$java HasCharacters "1 name"
Has characters
$java HasCharacters "10,000"
Ok
$java HasCharacters "na123me"
Has characters
$java HasCharacters "na 123, 000"
Has characters
答案 1 :(得分:5)
public static void main(String[] args)
{
Pattern p = Pattern.compile("^([^a-zA-Z]*([a-zA-Z]+)[^a-zA-Z]*)+$");
Matcher m = p.matcher("1 name");
Matcher m1 = p.matcher("10,000");
Matcher m2 = p.matcher("na123me");
Matcher m3 = p.matcher("na 123, 000");
Matcher m4 = p.matcher("13bbbb13jdfgjd43534 fkgdfkgjk34 rktekjg i54 ");
if (m.matches())
System.out.println(m.group(1));
if (m1.matches())
System.out.println(m1.group(1));
if(m2.matches())
System.out.println(m2.group(1));
if(m3.matches())
System.out.println(m3.group(1));
if (m4.matches())
System.out.println(m4.group(1));
}
上面应该匹配大小写中的任何字母。如果正则表达式返回匹配项,则该字符串中包含一个字母。
结果
1个名字
我是na 123,000
I54
不包含字母的语句与表达式不匹配。
答案 2 :(得分:2)
答案 3 :(得分:0)
使用此行,您可以检查您的字符串是否仅包含正则表达式给出的字符(在本例中为a,b,c,... z和A,B,C,... Z):
boolean doesMatch = "your string".matches( "[a-zA-Z]*" );