How to use regular expression in Android

前端 未结 5 843
天命终不由人
天命终不由人 2020-12-03 21:07

I have a numberDecimal EditText which I want to validate using a regular expression. In validation what I want is:

  1. Before the decimal point, th

5条回答
  •  佛祖请我去吃肉
    2020-12-03 21:31

    You could try something like this: ^[1-9][0-9]{0,2}(\\.\\d)?$. This should match any number which does not start with 0 (^[1-9]) and is followed by at most two numbers ([0-9]{0,2}). The regex also allows for an optional decimal value ((\\.\\d)?$).

    That being said, ideally you should parse the string value to a double or float and make what ever validations you need to make using the actual numerical value.

    To parse your string into a double value, you will need to use the Double.parseDouble(String s) like so: double myValue = Double.parseDouble(EditText.getText());

提交回复
热议问题