Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression help for InputFilter for EditText in Android

Tags:

regex

android

I need to implement an input filter for limiting numeral entry in the format 1234.35. That is, maximum four before . and two decimal places. I am using this regular expression pattern:

Pattern.compile("[0-9]{0,4}+((\\.[0-9]{0,2})?)||(\\.)?");

This works, but once I enter a number in the edit text and try to edit the values before the decimal places, I can't edit them. I can only delete them.

What is wrong?

like image 975
Arun Abraham Avatar asked Sep 06 '26 07:09

Arun Abraham


1 Answers

Based upon what you said and I think it looks like you were trying to do, I would use this regular expression:

^(\d{0,4})(\.\d{1,2})?$

It matches '0-4 digits' with or without 'a decimal point and two numbers' following them. If there is a decimal point, then either one or two digits must follow it. For instance: 5, 1234, 1234.56, .2, and .31 are all valid and matched by the expression, but .123, 1234., 1234.567, 12345, and . are NOT matched.

Alternatively, to allow numbers ending in decimals (like ., 1234., and the like), use this modification:

^(\d{0,4})(\.(\d{1,2})?)?$
like image 153
Code Jockey Avatar answered Sep 07 '26 21:09

Code Jockey