3
我正在尋找一些幫助,以獲得Android的正則表達式,可以防止更多的一個給定的十進制數前導零。目前,可以在實際數字之前添加任意數量的零。我正在尋找像0.1,11.04,0129這樣的數字的支持。提前Android的正則表達式/輸入過濾器,以防止多個領先的零
public class DecimalDigitsInputFilter implements InputFilter{
Pattern mPattern;
int maxDigitsBeforeDecimalPoint = 10;
int maxDigitsAfterDecimalPoint = 2;
public DecimalDigitsInputFilter(int digitsBeforeZero, int digitsAfterZero)
{
this.maxDigitsBeforeDecimalPoint = digitsBeforeZero;
this.maxDigitsAfterDecimalPoint = digitsAfterZero;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
int dend)
{
StringBuilder builder = new StringBuilder(dest);
builder.replace(dstart, dend, source.subSequence(start, end).toString());
if(!builder.toString().matches(
"(([0-9]{1})([0-9]{0," + (maxDigitsBeforeDecimalPoint - 1) + "})?)?(\\.[0-9]{0,"
+ maxDigitsAfterDecimalPoint + "})?"))
{
if(source.length() == 0)
return dest.subSequence(dstart, dend);
return "";
}
return null;
}}
感謝:這不應該被允許的數字是.9 0009999.33,0007
現有過濾器類,如下。
像這樣'^ \\ .. * |^[0] {2,} *'。? – SomeJavaGuy
看[這裏](http://pastebin.com/WchkxhgD) – pskink