Regex Flashcards
Learn Regex syntax
import re
str = “The rain in Spain will rain.”
x = re.findall(“rain”, str)
print(x)
[‘rain’, ‘rain’]
import re
str = “The rain in Spain will rain.”
x = re.findall(“rain|Spain”, str)
print(x)
[‘rain’, ‘Spain’, ‘rain’]
import re
str = “The rain in Spain will rain.”
x = re.findall(“^rain|Spain”, str)
print(x)
[‘Spain’]
import re
print(re.findall(“al{2}”, “fall”))
print(re.findall(“al{3}”, “fall”))
print(re.findall(“tal{2}”, “fall”))
[‘all’]
[]
[]
import re
str = “alll aboard the pain train”
x = re.search(“aboard”, str)
print(x.span())
(5, 11)
import re
str = “alll aboard the pain train”
x = re.search(“aboard”, str)
print(x.string)
#the .string gives you everything alll aboard the pain train
import re
print(re.findall(“[arn]”, “rainy”))
print(re.findall(“[^arn]”, “rainy”))
print(re.findall(“^[arn]”, “rainy”))
[‘r’, ‘a’, ‘n’]
[‘i’, ‘y’]
[‘r’]
import re
str = “rainy”
x = re.findall(“[arn]”, str)
print(x)
[‘r’, ‘a’, ‘n’]
import re
str = “8 times before 11:45 AM”
x = re.findall(“[0-5][0-9]”, str)
print(x)
#Returns anything with digits between 00 and 59 ['11', '45']
import re
str = “The rain in Spain”
x = re.findall(“\W”, str)
print(x)
#\W returns all non word characters, like white space [' ', ' ', ' ']
import re
str = “The rain in Spain”
x = re.findall(“\ASpain”, str)
print(x)
#\A checks if the string starts with something []
import re
print(re.findall(“a…s$”, “abs”))
print(re.findall(“a…s$”, “alias”))
print(re.findall(“a…s$”, “abyss”))
print(re.findall(“a…s$”, “abacus”))
#Three periods means three characters that aren't newline [] ['alias'] ['abyss'] []
import re
str = “a aabc daaaat”
x = re.findall(“a{2,3}”, str)
print(x)
#At least 2, at most 3 ['aa', 'aaa']
import re
print(re.findall("ma*n", "mn")) print(re.findall("ma*n", "man")) print(re.findall("ma*n", "maaan")) print(re.findall("ma*n", "main")) print(re.findall("ma*n", "woman"))
#* means zero or more occurrences of the a ['mn'] ['man'] ['maaan'] [] ['man']
import re
print(re.findall("ma+n", "mn")) print(re.findall("ma+n", "man")) print(re.findall("ma+n", "maaan")) print(re.findall("ma+n", "main")) print(re.findall("ma+n", "woman"))
#+ means 1 or more occurrences of the a [] ['man'] ['maaan'] [] ['man']