Python

Regular Expressions

37 flashcards · answers and spaced-repetition review in the KnowCard app

Greedy .* vs lazy .*? — what's the difference and when does it bite you?
Why should regex patterns almost always be written as raw strings r"..." instead of plain strings?
What does this print? ``` import re print(re.match(r"Python", "I love Python")) ```
re.match vs re.search vs re.fullmatch — what does each require of where the pattern sits in the string?
What does this print? ``` import re print(re.findall(r"P[Yy]thon", "Python or PYthon")) ```
What does this print? ``` import re print(re.findall(r"P([Yy])thon", "Python and PYthon")) ```
What does this print? ``` import re print(re.findall(r"P([Yy])th(.)n", "Python and PYthon")) ```
What does this print, and why isn't it three matches? ``` import re print(re.search(r"Py.*on", "Python Python Python").group()) ```
What does this print? ``` import re print(re.sub(r"[Jj]ava", "Python", "Java and java")) ```
What does this print? ``` import re print(re.sub(r"([Jj]ava)", r"\1 (a language)", "Take Java")) ```
What does this print? ``` import re m = re.match(r"(P[Yy])(th.n)", "Python") print(m.group(0), m.group(1), m.groups()) ```
What does this print? ``` import re m = re.match( r"(?P<a>P[Yy])(?P<b>th.n)", "Python") print(m.group("b"), m.groupdict()) ```
What does this print? ``` import re m = re.match(r"P(x)?(th)", "Pth") print(m.groups()) ```
What does this print? ``` import re print(re.findall(r"(?:ab)+", "ababab")) ```
In regex, what do \d, \w, and \s match — and their uppercase forms?
What does this print? ``` import re print(bool(re.search(r"a.c", "a\nc"))) print(bool(re.search(r"a.c", "a\nc", re.DOTALL))) ```
What does this print? ``` import re s = "ab\ncd" print(re.findall(r"^..", s)) print(re.findall(r"^..", s, re.MULTILINE)) ```
What does this print? ``` import re print(re.match(r"python", "Python")) print(re.match(r"python", "Python", re.IGNORECASE)) ```
What does this print? ``` import re print(re.findall(r"P[Yy]{2}thon", "Pyython Python")) ```
What does \b mean in a regex, and what's the bug in re.search("\bword", text)?
Why compile a regex with re.compile, and what does re.escape protect against?
What's the danger in a pattern like r"(a+)+$" run against "aaaa...aaab"?
What does the conditional extension (?(id/name)yes-pattern|no-pattern) do?
What does this print? import re print(re.split(r"(,)\s", "a, b, c"))
Besides a replacement string, what else can you pass as repl to re.sub, and what does re.subn add?
Given a match object m, how do you find where a group matched in the input string?
What does the re.VERBOSE (re.X) flag change about how a pattern is read?
Is a regular expression a good tool for parsing arbitrary HTML?
Inside a character class [...], which characters keep a special meaning — and what does [.?] match?
Why can \w match a German umlaut like 'ä' with one pattern but not another?
How do the anchors \A, \Z, and \B differ from ^ and $?
In P(ython|eter) rocks, what does the | apply to — and what's the precedence gotcha?
How do you write a pattern that matches the same text twice, e.g. a doubled word like 'the the'?
How can you turn on IGNORECASE for only part of a regex, not the whole thing?
What do the lookahead extensions (?=...) and (?!...) do — and why use one over \w+ Miller?
What are (?<=...) and (?<!...), and how do they differ from lookahead?
What's the difference between [abc] and (abc) in a regex?

Start learning today

Free to start — download the app or use it in your browser.

Get it on App StoreGet it on Google Play