Skip to content

Instantly share code, notes, and snippets.

Last active May 10, 2020
What would you like to do?
Floating point regex: A regular expression to match any valid python floating point value.
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
# See it in action
import re
regex = r"""
test_str = ("0.2 2.1 3.1 ab 3 c abc23\n"
"1 34345 234 -121\n"
"-1e+ -1e+0 1e-1")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("\tGroup {groupNum} found at {start}-{end}: {group}".format(
groupNum=groupNum, start=match.start(groupNum), end=match.end(groupNum),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment