Last active
October 28, 2019 15:59
-
-
Save prerakmody/0c629bed1d8f0b6c4e1788b9425da65e to your computer and use it in GitHub Desktop.
Regex Samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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