0

I am confused about the pattern used in my Regular Expression. What I want is before decimal the max no of digit the user want to enter is three and the digit should not start with zero. After entering the digit before decimal point which can be three-digit or two-digit or one-digit but can not start with zero. I want to allow users to enter one digit after the decimal point and it should be no more than one. So what will be the pattern for these?

Example:

Number Allow to enter: 1.3, 22.3, 333.4, 999.6

Number Not allowed to enter: 0, 0.1, .1, 4444.67, 333.78

Code I have Used

tempEditText.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(3,1)});
class DecimalDigitsInputFilter implements InputFilter
{
    Pattern mPattern;

    public DecimalDigitsInputFilter(int digitsBeforeZero, int digitsAfterZero)
    {
        mPattern = Pattern.compile("[0-9]{0," + (digitsBeforeZero - 1) + "}+((\\.[0-9]{0," + (digitsAfterZero - 1) + "})?)");
    }
    
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
    {
        Matcher matcher = mPattern.matcher(dest);
        if(!matcher.matches())
        {
            return "";
        }
        return null;
    }
}
iknow
  • 8,358
  • 12
  • 41
  • 68
AndroidDev
  • 4,521
  • 24
  • 78
  • 126

3 Answers3

1

I think that the pattern you want is:

^[1-9]\d{0,2}\.\d?$

Explanation:

^        // Start of string
[1-9]    // First digit can't be 0
\d{0,2}  // Zero, one, or two more digits after first digit
\.       // Literal decimal point
\d?      // Optional single digit after decimal point
$        // End of string

In your code, it would be:

mPattern = Pattern.compile("^[1-9]\\d{0," + (digitsBeforeZero - 1)
           + "}(\\.\\d{0," + digitsAfterZero + "})?$");

The parentheses that you had around the decimal point and the trailing digit means that a number like "1." would fail to match. Here's a version that makes the decimal point required and the digit(s) afterwards optional:

mPattern = Pattern.compile("^[1-9]\\d{0," + (digitsBeforeZero - 1)
           + "}\\.\\d{0," + digitsAfterZero + "}?$");
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • What is d here.Can u please edited it in my code..Actually m stuck in it from an hour. – AndroidDev Apr 25 '12 at 05:28
  • You should group the dot and the last digit together, otherwise it allows "1." – stema Apr 25 '12 at 05:30
  • @Anshuman - `\d` is the same as `[0-9]`. It means "any digit character". You'll have to double up the `\` characters in the pattern to make it a Java string: `"^[1-9]\\d{0,2}\\.\\d?$"` – Ted Hopp Apr 25 '12 at 05:30
  • @stema - I understood from OP's description that "1." would be okay. If I got it wrong, then you're right -- the dot and last digit should be grouped. – Ted Hopp Apr 25 '12 at 05:31
  • @Ted Hopp When i tried to used these pattern it crashes the app and shows the following error java.util.regex.PatternSyntaxException: Syntax error U_REGEX_MISMATCHED_PAREN – AndroidDev Apr 25 '12 at 05:37
  • OK, thats my assumption, its neither in his allowed numbers nor in his not allowed. – stema Apr 25 '12 at 05:38
  • @Anshuman - Double check for typos. Count left and right parentheses. – Ted Hopp Apr 25 '12 at 05:39
  • @stema whenever i use the pattern u have provided Pattern.compile("^[1-9]\\d{0," + (digitsBeforeZero - 1) + "}(\\.\\d{0," + digitsAfterZero + "})?$"); it solve the crash error but it don't allow to enter any digit in the EditBox – AndroidDev Apr 25 '12 at 05:53
  • @Anshuman - I think that your filter logic is wrong: you should not be trying to match `dest`. When the user types the first character, `dest` will be the empty string and `source` will be the typed character. The match will reject an empty string (since it requires a leading digit). You need to test whether the text after the replacement/insertion/deletion is acceptable. – Ted Hopp Apr 25 '12 at 06:42
  • @Ted Hopp. i tried a lot but its not working...can u please help me to solve these out.. – AndroidDev Apr 25 '12 at 09:35
  • @Anshuman - You are describing, I think, a different problem now. I suggest that you post a new question with your current code. Without seeing what you're doing, it's hard to offer suggestions. – Ted Hopp Apr 25 '12 at 15:58
  • @Ted Hopp I have posted a new question regarding this and explain what actually i want link for the question http://stackoverflow.com/questions/10327901/how-to-use-regular-expression-in-android – AndroidDev Apr 26 '12 at 05:54
0
Pattern.compile("[1-9]{0," + (digitsBeforeZero - 1) + "}+((\\.[0-9]{0," + (digitsAfterZero - 1) + "})?)"

try this....

Vishnu
  • 70
  • 2
  • 12
0

You have following problems:

"[0-9]{0," + (digitsBeforeZero - 1) + "}+((\\.[0-9]{0," + (digitsAfterZero - 1) + "})?)"
 ^^^^^                                              ^
  1                                                 2
  1. Your pattern allows 0 at the start, but you don't want a leading 0

  2. You allow 0 digits after the dot, but you don't want it.

  3. You need anchors around your regex to avoid partial matches ("Foo123.4Bar" would be allowed)

So try this

mPattern = Pattern.compile("^[1-9][0-9]{0," + (digitsBeforeZero - 1) + "}((\\.[0-9]{1," + (digitsAfterZero - 1) + "})?)$");
                                                                                                          ^^^^
                                                                                                       really -1 ???
stema
  • 90,351
  • 20
  • 107
  • 135
  • Possessive quantifiers are supported in Android. – Ted Hopp Apr 25 '12 at 05:40
  • @stema it don't allow to enter digit in the EditBox – AndroidDev Apr 25 '12 at 05:54
  • @Anshuman Check in your debugger what your pattern looks like (you can get it from the compiled pattern with `mPattern.pattern()`) and check, also in the debugger, how the string looks like, that you try to match. – stema Apr 25 '12 at 06:09
  • @ stema Actually if the edit box is blank it donesn't allow to write anything in the edit box. But if there is already an number in the edit box then it allow to write in edit box..why this is happen – AndroidDev Apr 25 '12 at 11:23