Skip to content

Instantly share code, notes, and snippets.

@Lvl4Sword
Last active March 20, 2016 08:43
Show Gist options
  • Save Lvl4Sword/b973a436d3ed21095e2c to your computer and use it in GitHub Desktop.
Save Lvl4Sword/b973a436d3ed21095e2c to your computer and use it in GitHub Desktop.
Find Numbers In A String
# This finds positive, negative, and float numbers.
# They're added into a list by what was found first
# and then presented to the user.
# Let me know if you find any issues.
import re
# this prints ['0', '-10', '10.0', '10.999999999999999']
# which is what's expected :-)
the_string = "single 0 // negative -10 // float 10.0 // long float 10.999999999999999"
pattern = re.compile(r"(-?\d+(?:\.\d+)?)")
numbers_found = pattern.findall(the_string)
print(numbers_found)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment