The code is:
Pattern p = Pattern.compile("(\\d+(\\.\\d+)?)");
The code is:
Pattern p = Pattern.compile("(\\d+(\\.\\d+)?)");
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.
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
).