Regular expression to check only numbers and points

I want a jge regex expression that accepts only numbers and periods.

eg,

             1.1.1 ----valid
             1.1   ----valid
             1.1.1.1---valid
             1.    ----not valid

Points should not be in the starting position or in the end position.

+5
source share
5 answers

I think this is what you want:

^\d+(\.\d+)*$

Explanation: It accepts numbers separated by periods; it begins and ends with a number; a number may have several digits; a single number without dots is also accepted.

Option without multiple digits:

^\d(\.\d)*$

Options where at least one point is required:

^\d+(\.\d+)+$
^\d(\.\d)+$

Don't forget that in Java you need to escape the \ character, so the code will look like this:

Pattern NUMBERS_WITH_DOTS = Pattern.compile("^\\d+(\\.\\d+)*$");
+17
source

So you need a regular expression that wants numbers and periods, but starts and ends with a number?

"[0-9][0-9.]*[0-9]"

, 1. , .

+1
"^\\d(\\.\\d)*$"

1     ----valid (if it must be not valid, replace `*` => `+` )
1.1.1 ----valid
1.1   ----valid
1.1.1.1---valid
1.    ----not valid
11.1.1 ---not valid (if it must be valid, add `+` after each `d`) 
0
source
<!DOCTYPE html>
<html>
<body>

<p>RegEx to allow digits and dot</p>
Number: <input type="text" id="fname" onkeyup="myFunction()">

<script>
function myFunction() {
    var x = document.getElementById("fname");
    x.value = x.value.replace(/[^0-9\.]/g,"");
}
</script>

</body>
</html>
0
source

I think this is what you want:

Pattern.compile("(([0-9](\\.[0-9]*))?){1,13}(\\.[0-9]*)?(\\.[0-9]*)?(\\.[0-9]*)?", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL | Pattern.MULTILINE);

Explanation: It accepts numbers separated by periods; it begins and ends with a number; a number may have several digits; one number without dots is not accepted.

The result is this:

  • 1.1
  • 1.12
  • 1.1.5
  • 1.15.1.4
0
source

All Articles