你能帮我找一个正则表达式,找出字符串的第一个字母是否是字母。
这里我的要求是阻止用户在编辑文本中键入除字母之外的其他字符作为第一个字母。例如,像这样,
edtTxtFirstname.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(30),
new CharacterInputFilter(Pattern.compile("^[a-zA-Z]")) });
由于
JRH
答案 0 :(得分:0)
检查
String strRegExpr= "[a-zA-Z]";
if (youstr.matches(strRegExpr))
{
// your entered value is Alphabetic
Log.i("Character is Alphabate", "Check this"+yourstr);
}
else
{
// your entered value is Numeric
Log.i("Character is Numaric", "Check this"+yourstr);
}
答案 1 :(得分:0)
像这样创建自己的过滤器
InputFilter myFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (dstart == 0) {
try {
Character c = source.charAt(0);
if (Character.isLetter(c) && Character.isUpperCase(c)) {
return null;
} else {
if (Character.isLetter(c) && Character.isLowerCase(c))
return "" + Character.toUpperCase(c);
}
} catch (Exception e) {
}
}
return null;
}
};
editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(30), myFilter });