Regex Flashcards

Learn Regex syntax

1
Q

import re

str = “The rain in Spain will rain.”
x = re.findall(“rain”, str)
print(x)

A

[‘rain’, ‘rain’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

import re

str = “The rain in Spain will rain.”
x = re.findall(“rain|Spain”, str)
print(x)

A

[‘rain’, ‘Spain’, ‘rain’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

import re

str = “The rain in Spain will rain.”
x = re.findall(“^rain|Spain”, str)
print(x)

A

[‘Spain’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

import re

print(re.findall(“al{2}”, “fall”))
print(re.findall(“al{3}”, “fall”))
print(re.findall(“tal{2}”, “fall”))

A

[‘all’]
[]
[]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

import re

str = “alll aboard the pain train”
x = re.search(“aboard”, str)
print(x.span())

A

(5, 11)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

import re

str = “alll aboard the pain train”
x = re.search(“aboard”, str)
print(x.string)

A
#the .string gives you everything
alll aboard the pain train
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

import re

print(re.findall(“[arn]”, “rainy”))
print(re.findall(“[^arn]”, “rainy”))
print(re.findall(“^[arn]”, “rainy”))

A

[‘r’, ‘a’, ‘n’]
[‘i’, ‘y’]
[‘r’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

import re

str = “rainy”
x = re.findall(“[arn]”, str)
print(x)

A

[‘r’, ‘a’, ‘n’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

import re

str = “8 times before 11:45 AM”
x = re.findall(“[0-5][0-9]”, str)
print(x)

A
#Returns anything with digits between 00 and 59
['11', '45']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

import re

str = “The rain in Spain”
x = re.findall(“\W”, str)
print(x)

A
#\W returns all non word characters, like white space
[' ', ' ', ' ']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

import re

str = “The rain in Spain”
x = re.findall(“\ASpain”, str)
print(x)

A
#\A checks if the string starts with something
[]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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”))

A
#Three periods means three characters that aren't newline
[]
['alias']
['abyss']
[]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

import re

str = “a aabc daaaat”
x = re.findall(“a{2,3}”, str)
print(x)

A
#At least 2, at most 3
['aa', 'aaa']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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"))
A
#* means zero or more occurrences of the a
['mn']
['man']
['maaan']
[]
['man']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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"))
A
#+ means 1 or more occurrences of the a
[]
['man']
['maaan']
[]
['man']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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"))
A
#? means zero or 1 occurrence of the a
['mn']
['man']
[]
[]
['man']
17
Q

import re

print(re.findall(r”\bfoo”,”football”))
print(re.findall(r”\bfoo”,”a football”))
print(re.findall(r”\bfoo”,”afootball”))

A

checks for foo at the beginning of words

[‘foo’]
[‘foo’]
[]

18
Q

import re

print(re.findall(r”foo\b”,”the foo”))
print(re.findall(r”foo\b”,”the afoo test”))
print(re.findall(r”foo\b”,”the afootest”))

A

checks for foo at the end of words

[‘foo’]
[‘foo’]
[]

19
Q

import re

print(re.findall(r”\Bfoo”,”football”))
print(re.findall(r”\Bfoo”,”a football”))
print(re.findall(r”\Bfoo”,”afootball”))

A

makes sure foo is present, but NOT at the beginning of a word

[]
[]
[‘foo’]

20
Q

import re

print(re.findall(r”foo\B”,”the foo”))
print(re.findall(r”foo\B”,”the afoo test”))
print(re.findall(r”foo\B”,”the afootest”))

A

makes sure foo is present, but NOT at the end of a word

[]
[]
[‘foo’]

21
Q

import re

print(re.findall(“\d”,”12a!bc3”))
print(re.findall(“\d”,”Pyth@n”))

A

\d makes a match for all digits

[‘1’, ‘2’, ‘3’]
[]

22
Q

import re

print(re.findall(“\D”,”1a!b34’5”))
print(re.findall(“\D”,”1345”))

A

\D makes a match for all non-digits

[‘a’, ‘!’, ‘b’, “’”]
[]

23
Q

import re

print(re.findall(“\s”,”Python”))
print(re.findall(“\s”,”Python yo”))

A

Returns whitespace

[]
[’ ‘]

24
Q

import re

print(re.findall(“\S”,”a b”))
print(re.findall(“\s”,” “))

A

\S returns everything that isn’t whitespace

#\s returns whitespace
[‘a’, ‘b’]
[’ ‘]

25
Q

import re

print(re.findall(“\w”,”12&’: ;c”))
print(re.findall(“\w”,”’%> !”))

A

Returns all alphanumeric characters

[‘1’, ‘2’, ‘c’]
[]

26
Q

import re

print(re.findall(“\W”,”1a2%c”))
print(re.findall(“\W”,”’%> !”))

A

Returns all non-alphanumeric characters

[’%’]
[”’”, ‘%’, ‘>’, ‘ ‘, ‘!’]

27
Q

import re

print(re.findall(“Python\Z”,”I like Python”))
print(re.findall(“Python\Z”,”I like python”))
print(re.findall(“Python\Z”,”Python is fun”))

A

Returns ‘Python’ if the string ends with it

[‘Python’]
[]
[]