-1

The code is:

Pattern p = Pattern.compile("(\\d+(\\.\\d+)?)");
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Devin
  • 17
  • 1
  • 1
  • 2

2 Answers2

2
a. \d implies digit.
b. + sign implies one or more occurance of previous character.
c. \. -> since . is a special character in regex, we have to escape it with \.
d. Also, \ is a special escape character in java , hence from java perspective we need to add an additional \ to escape the backslash (\).

Thus, the pattern will reprent any number like: 0.01, 0.001, 1.0001, 100.00001 and so on. Basically any decimal number with a digit before and after the decimal point.

akshaya pandey
  • 997
  • 6
  • 16
0

The regex is a simplified version to recognize floating-point numbers: At least one digit optionally followed by a dot and at least a digit.

It's simplified because it only covers only number without a sign (i.e. only positive numbers, because you can't provide a - minus sign), it allows number presentations that are considered invalid, e.g. 000123.123 and lacks the support of numbers written in scientific syntax (e.g. 1.234e56).

Lothar
  • 5,323
  • 1
  • 11
  • 27