Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@prerakmody
Last active October 28, 2019 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prerakmody/0c629bed1d8f0b6c4e1788b9425da65e to your computer and use it in GitHub Desktop.
Save prerakmody/0c629bed1d8f0b6c4e1788b9425da65e to your computer and use it in GitHub Desktop.
Regex Samples
"""
Ref: https://regexr.com/
"""
import re
# Sample 1 - Finding between two chars
str1 = 'M 50.77925000000002 398.604 a 2.5 2.5 0 1 0 5 0 a 2.5 2.5 0 1 0 -5 0' # find string between 'M' and 'a'
str2 = 'M226.31525000000002,401.996a2.5,2.5 0 1,0 5,0a2.5,2.5 0 1,0 -5,0'
out1 = [s.strip() for s in re.findall(r'M([\d,. ]+)a', str1)]
out2 = [s.strip() for s in re.findall(r'M([\d,. ]+)a', str2)]
# Sample 2 - Extract floating point numbers
str1 = '226.31525000000002,401.996'
str2 = '50.77925000000002 398.604'
out1 = [float(s) for s in re.findall(r'[\d]*\.+[\d]*', str1)]
out2 = [float(s) for s in re.findall(r'[\d]*\.+[\d]*', str2)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment