Skip to content

Instantly share code, notes, and snippets.

@vicro
Last active November 10, 2016 02:30
Show Gist options
  • Save vicro/16e8c80a40fc53e2b8d4fe6abf6c5df2 to your computer and use it in GitHub Desktop.
Save vicro/16e8c80a40fc53e2b8d4fe6abf6c5df2 to your computer and use it in GitHub Desktop.
Python REGEX quick ref
#import regex module
import re
#Simple match of a phone number 090-1234-5678
print("####### Simple match of a phone number 090-1234-5678 #######")
match = re.match( r"\d{3}-\d{4}-\d{4}", "090-1234-5678" )
print(match)
#No match
print("####### No match #######")
match = re.match( r"\d{3}-\d{4}-\d{4}", "090-XXXX-5678" )
print(match)
#With groups
print("####### With groups #######")
match = re.match( r"(\d{3})-(\d{4})-(\d{4})", "090-1234-5678" )
print(match)
print(match.group(0)) #group 0 is always the whole match string
print(match.group(1)) #group 1
print(match.group(2)) #group 2
print(match.group(3)) #group 3
#Compile regex for easy reuse
print("####### Compile regex for easy reuse #######")
phone_regex = re.compile( r"(\d{3})-(\d{4})-(\d{4})" )
print(phone_regex.match("123-1234-1234"))
print(phone_regex.match("123-1234-abcd"))
#Replace groups
print("####### Replace groups #######")
print( re.sub(phone_regex, r"first: \1 second:\2 third:\3", "090-1234-5678") )
print( phone_regex.sub(r"first: \1 second:\2 third:\3", "090-1234-5678") )
#Multiline regex (SINGLE LINE IS DEFAULT!!)
print("####### Multiline regex (SINGLE LINE IS DEFAULT!!) #######")
pattern = re.compile( r"title:(\w+)\ndesc:(\w+)", re.MULTILINE )
match = pattern.match("title:Foo\ndesc:Bar")
print( match.group(1) )
print( match.group(2) )
#Named groups
print("####### Named groups #######")
match = re.match( r"(?P<variable>\w+)\s*=\s*(?P<value>\w+)", "x = 10" )
print(match.group("variable"))
print(match.group("value"))
#Verbose mode (with comments and ignored whitespace in regex string)
print("####### Verbose mode #######")
pattern = re.compile( r"""^\s* #whitespace at the beginning of line
\w+ #a word
\s+ #followed by whitespace
\d{3} #then a 3 digit number
""", re.VERBOSE )
print(pattern.match("hello 123"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment