Title |
Test
Find
Real Float Number
|
Expression |
[+-]?+(?>\d++\.?+\d*+|\d*+\.?+\d++) |
Description |
This regex is designed to fail fast (using possessive quantifiers) while still matching a floating decimal number all the time. Won't match scientific notation or fractions. Can be combined with other patterns.
Note: This site's tester does not support them.
If your language does not support possessive quantifiers try the following:
(?>[+-]?)(?>(?>\d+)(?>\.?)(?>\d*)|(?>\d*)(?>\.?)(?>\d+)) |
Matches |
+7 | -7.7 | .7 | 7. | etc |
Non-Matches |
1/7 | 7e10 |
Author |
Rating:
Trinithis
|
Source |
|
Your Rating |
|
Title: Real Float Number
Name: Rick S
Date: 8/1/2013 12:45:26 PM
Comment:
Thanks for this post. I was unclear how to speed this up so this was exactly what I was looking for.
I don't think you need to look for digits preceding the decimal point in the second alternative because this case will be captured by the first alternative. Also if it gets to the second alternative then we should always see a decimal point. So the expression becomes.
(?>[+-]?)(?>(?>\d+)(?>\.?)(?>\d*)|(?>\d*)(?>\.)(?>\d+))