Glob and Regex: Practice Questions
For each Python snippet, write its output. For each shell snippet, write the output of its final command. If it prints nothing, write “No Output”.
Each snippet starts in a new, empty directory.
Question 1
touch draft.txt notes.txt image.png
ls *.txtAnswer
draft.txt
notes.txt
Question 2
touch run1.csv run12.csv runA.csv
ls run?.csvAnswer
run1.csv
runA.csv
Question 3
touch chapter1.md chapter2.md chapter3.md
ls chapter[!2].mdAnswer
chapter1.md
chapter3.md
Question 4
mkdir north south
touch north/r1.log north/r2.txt south/r3.log
ls */*.logAnswer
north/r1.log
south/r3.log
Question 5
touch data-a.csv data-b.csv data-long.csv
ls data-?.csvAnswer
data-a.csv
data-b.csv
Question 6
printf 'red\nblue\n' > notes.txt
printf 'green\n' > notes.csv
cat notes.* | wc -lAnswer
3
Question 7
import re
print(bool(re.search(r'art', 'cartoon')))
print(bool(re.search(r'art', 'drawing')))Answer
True
False
Question 8
import re
for text in ['v1.2', 'v1x2', 'v12']:
print(bool(re.search(r'1.2', text)), bool(re.search(r'1\.2', text)))Answer
True True
True False
False False
Question 9
import re
for tag in ['A4', 'a4', '44', '_4']:
print(bool(re.search(r'[A-Z][0-9]', tag)))Answer
True
False
False
False
Question 10
import re
for word in ['h', 'ha', 'haaa', 'haha']:
print(bool(re.search(r'^ha+$', word)))Answer
False
True
True
False
Question 11
import re
for text in ['http', 'https', 'httpss', 'htt']:
print(bool(re.search(r'^https?$', text)))Answer
True
True
False
False
Question 12
import re
for word in ['start', 'restart', 'starting']:
print(bool(re.search(r'^start', word)), bool(re.search(r'start$', word)))Answer
True True
False True
True False
Question 13
import re
for text in ['abc', 'a1', '_', '']:
print(bool(re.search(r'^[^0-9]+$', text)))Answer
True
False
True
False
Question 14
import re
messages = ['error: disk', 'all clear', 'warning: heat', 'no errors']
selected = [message for message in messages if re.search(r'error|warn', message)]
print(selected)Answer
['error: disk', 'warning: heat', 'no errors']
Question 15
printf 'open\nclosed\nreopened\nheld\n' > status.txt
grep -E 'open|held' status.txtAnswer
open
reopened
held
Question 16
printf 'error error\nok\n' > today.log
printf 'warning\nerror\n' > old.log
cat *.log | grep 'error' | wc -lAnswer
2
Question 17
import re
text = 'room 12, room 305, room 7'
print(re.findall(r'\d+', text))
print(re.findall(r'\d{2}', text))Answer
['12', '305', '7']
['12', '30']
Question 18
import re
text = 'red green\tblue\nred'
print(re.findall(r'\S+', text))Answer
['red', 'green', 'blue', 'red']
Question 19
import re
text = 'size=12 price=30 size=8 size=12'
print(re.findall(r'size=(\d+)', text))Answer
['12', '8', '12']
Question 20
import re
match = re.search(r'score=(\d+)', 'name=Lee score=17')
if match:
print(match.group(0))
print(match.group(1))Answer
score=17
17
Question 21
import re
for text in ['room=12', 'room=unknown', 'room=7']:
match = re.search(r'room=(\d+)', text)
if match:
print(int(match.group(1)) + 1)
else:
print('missing')Answer
13
missing
8
Question 22
import re
pairs = re.findall(r'([a-z]+):(\d+)', 'tea:3 rice:12 tea:5')
print(pairs[0])
print(pairs[1][1])Answer
('tea', '3')
12
Question 23
import re
numbers = re.findall(r'points=(\d+)', 'points=8 bonus=20 points=12')
print(numbers)
print(sum([int(number) for number in numbers]))Answer
['8', '12']
20
Question 24
import re
pattern = r'[A-Z]{2}[0-9]{3}'
for code in ['AB123', 'xAB123', 'AB1234', 'Ab123']:
print(bool(re.search(pattern, code)), bool(re.fullmatch(pattern, code)))Answer
True True
True False
True False
False False
Question 25
import re
names = ['notes.txt', 'notesXtxt', 'notes.txt.old', 'draft.txt']
valid = [name for name in names if re.fullmatch(r'\w+\.txt', name)]
print(valid)Answer
['notes.txt', 'draft.txt']
Question 26
import re
text = 'Order 12 has 3 items.'
redacted = re.sub(r'\d+', '#', text)
print(redacted)
print(text)Answer
Order # has # items.
Order 12 has 3 items.
Question 27
import re
text = 'A12 B3'
print(re.sub(r'\d', '#', text))
print(re.sub(r'\d+', '#', text))Answer
A## B#
A# B#
Question 28
import re
text = 'tea=3 rice=12'
print(re.sub(r'(\w+)=(\d+)', r'\g<1>: \g<2>', text))Answer
tea: 3 rice: 12
Question 29
import re
text = '03/14 and 11/02'
print(re.sub(r'(\d{2})/(\d{2})', r'\g<2>-\g<1>', text))Answer
14-03 and 02-11
Question 30
import re
text = 'row 12\tseat 3'
clean = re.sub(r'\s+', ' ', text)
print(clean)
print(re.findall(r'\d+', clean))Answer
row 12 seat 3
['12', '3']
Question 31
import re
text = 'Room 7'
re.sub(r'\d', '?', text)
print(text)Answer
Room 7
Question 32
import re
text = 'P2 P10 X3'
clean = re.sub(r'P(\d+)', r'item=\g<1>', text)
values = re.findall(r'item=(\d+)', clean)
print(sum([int(value) for value in values]))Answer
12