How to check a line contains only digits and one occurrence of a decimal point?

My idea is something like this, but I don't know the correct code

if (mystring.matches("[0-9.]+")){ //do something here }else{ //do something here } 

I think I'm almost there. The only problem is that there can be several decimal points in the string. I searched for this answer, but could not find.

+7
java string decimal regex
source share
8 answers

If you want to → make sure that the AND number has only one decimal place <try this RegEx instead:

 if(mystring.matches("^[0-9]*\\.?[0-9]*$")) { // Do something } else { // Do something else } 

This RegEx states:

  • A value of ^ means the line should begin with this.
  • Next is none or more digits (this does this).
  • Optionally, there can be one decimal character (this does).
  • Follow by none or more digits (this does this).
  • And $ means it should end with that.

Please note that marker point number 2 is designed to catch someone entering ".02", for example.

If this is not valid, enter RegEx: "^[0-9]+\\.?[0-9]*$"

  • The only difference is the + sign. This will make the decimal digit contain the digit: 0.02
+9
source share

I think using regular expressions complicates the answer. A simpler approach is to use indexOf() and substring() :

 int index = mystring.indexOf("."); if(index != -1) { // Contains a decimal point if (mystring.substring(index + 1).indexOf(".") == -1) { // Contains only one decimal points } else { // Contains more than one decimal point } } else { // Contains no decimal points } 
+2
source share

You can use indexOf() and lastIndexOf() :

 int first = str.indexOf("."); if ( (first >= 0) && (first - str.lastIndexOf(".")) == 0) { // only one decimal point } else { // no decimal point or more than one decimal point } 
+2
source share

Protozoa

Example:

 "123.45".split(".").length(); 
+1
source share

If you want to check if the number (positive) has one dot, and if you want to use the regular expression, you must escape the dot because the dot means "any char" :-)

see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

 Predefined character classes . Any character (may or may not match line terminators) \d A digit: [0-9] \DA non-digit: [^0-9] \s A whitespace character: [ \t\n\x0B\f\r] \SA non-whitespace character: [^\s] \w A word character: [a-zA-Z_0-9] \WA non-word character: [^\w] 

so you can use something like

 System.out.println(s.matches("[0-9]+\\.[0-9]+")); 

ps. this will correspond to a number, for example, 01.1. I just want to illustrate \\.

+1
source share
 int count=0; For(int i=0;i<mystring.length();i++){ if(mystring.charAt(i) == '/.') count++; } if(count!=1) return false; 
0
source share

Use RegEx below to solve your problem

  • allow 2 decimal places (e.g. 0.00 to 9.99)

     ^[0-9]{1}[.]{1}[0-9]{2}$ This RegEx states: 1. ^ means the string must start with this. 2. [0-9] accept 0 to 9 digit. 3. {1} number length is one. 4. [.] accept next character dot. 5. [0-9] accept 0 to 9 digit. 6. {2} number length is one. 
  • allow 1 decimal number (e.g. 0.0 to 9.9)

     ^[0-9]{1}[.]{1}[0-9]{1}$ This RegEx states: 1. ^ means the string must start with this. 2. [0-9] accept 0 to 9 digit. 3. {1} number length is one. 4. [.] accept next character dot. 5. [0-9] accept 0 to 9 digit. 6. {1} number length is one. 
0
source share

I create myself to solve exactly the problem of the issue. I shared with you guys a regular expression:

^(\d)*(\.)?([0-9]{1})?$

Look at Online Regex to work fine

Refer to the documentation if you want to continue customizing your regular expression

Documentation

0
source share

All Articles