The following regex does this, although it's a little cryptic:
-?(?:\d+())?(?:\.\d*())?(?:e-?\d+())?(?:\2|\1\3)
Explanation:
There are three parts for a number (integer part, fractional part and exponential part). If the fractional part is present, it is a float , but if it is not, the number can still be a float when the exponential part follows.
This means that you must first make all three parts optional in a regular expression. But then we need to create rules that accurately determine which parts should be there to make a valid float.
Fortunately, there is a trick that allows us to do this. An empty capture group ( () ) always matches (empty string). Backlink to this group ( \1 ) is performed only if the group participated in the match. By inserting () in each of the optional groups, we can later check whether the necessary parts participating in the match participated.
For example, in Python:
regex = re.compile(r""" -?
Test suite:
>>> [match.group(0) for match in ... regex.finditer("1 1.1 .1 1. 1e1 1.04E-1 -.1 -1. e1 .1e1")] ['1.1', '.1', '1.', '1e1', '1.04E-1', '-.1', '-1.', '.1e1']
source share