用正则表达式来判断文字是否仅为字母a-zA-Z1-9_的组合.


下面是一个简单的模块.用正则表达式来判断文字是否仅为字母a-zA-Z1-(的组合.


 

public Boolean isCharAndNumic(String str) {
  Boolean is = false;
  Pattern pt = Pattern.compile("^[0-9a-zA-Z]+$");
  Matcher mt = pt.matcher(str);
  if (mt.matches()) {
    is = true;
    }
  return is;
}


如果还需要加多一个判断下划线的"_".那么很容易修改.

Pattern pt = Pattern.compile("^[0-9a-zA-Z]+$");

在这句话的里面加多"_"在Z的后面就可以啦.

Pattern pt = Pattern.compile("^[0-9a-zA-Z _]+$");

 

public Boolean isCharNumicAndUnderline(String str) {
  Boolean is = false;
  Pattern pt = Pattern.compile("^[0-9a-zA-Z_]+$");
  Matcher mt = pt.matcher(str);
  if (mt.matches()) {
   is = true;
  }
  return is;
 }


你可能感兴趣的:(android)