正则式汇总
1、判断金额的正则式
^([1-9]\d{0,9}|0)([.]?|(\.\d{1,2})?)$
mItem4TypeTextInput.setFilters(new InputFilter[]{new FilterUtil(4)});
/**
* 作者:小胖
* 创建时间 2018/8/27
* 功能描述:根据正则式过滤
*/
public class FilterUtil implements InputFilter {
//小数点后个数
private int num;
public FilterUtil(int num) {
this.num = num;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String sourceText = source.toString();
String destText = dest.toString();
//验证删除等按键
if (TextUtils.isEmpty(sourceText)) {
return "";
}
if (!regularPhone(destText + sourceText)) {
return "";
}
return sourceText;
}
private boolean match(String regex, String str) {
if (TextUtils.isEmpty(str))
return false;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}
/**
* 电话号码正则表达式
*
* @param phoneNumber
* @return
*/
public boolean regularPhone(String phoneNumber) {
String regular = "^([1-9]\\d{0,9}|0)([.]?|(\\.\\d{1," + num + "})?)$";
return match(regular, phoneNumber);
}
}