匹配数字和字母组合,数字和字母至少出现一次,只匹配1a,1q1,a1,a1a,1q2q2ws,w1w2e3r4r之类的,不匹配11,aa,a,1,"",这种的。
package com.abin.lee.servlet.regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyRegex {
public static boolean StringResult(String str)throws Exception{
String regex="^(\\d+[a-z]+[0-9a-z]*)|([a-z]+\\d[0-9a-z]*)$";
// String regex="^(\\d+[a-z]{1}[0-9a-zA-Z]*)|([a-z]+\\d[0-9a-zA-Z]*)$";
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(str);
boolean flag=matcher.matches();
return flag;
}
public static void main(String[] args) throws Exception{
String str="aa1as12ds3232ds2d22";
boolean result=StringResult(str);
System.out.println("result="+result);
}
}