Words and Tokens
Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2
33 cards in “2.6 Regular expressions” · whole deck
2.6 Regular expressions
- What is a regular expression, formally and practically?Formally, an algebraic notation for characterizing a set of strings. Practically, a pattern language for searching text and specifying changes to it; used in grep, editors, every programming language, and BPE pretokenization.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 48
- What does Python’s
re.search(pattern, string)return?The first match of the pattern inside the string.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 48
- Why are regexes in Python usually written as raw strings,
r"..."?In raw strings backslashes are treated literally, so regex escapes like\bor\dreach the regex engine unchanged.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 48
- What do square brackets do in a regex? Give examples.Character disjunction: match any one of the characters inside.
[mM]arymatches Mary or mary;[abc]matches a, b or c.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 49
- What does a dash inside brackets mean, as in
[b-g]?A range: any one character from b to g.[A-Z]is an uppercase letter,[0-9]a digit.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 49
- What does
[^a]mean, and when does^inside brackets mean something else?Any single character except a. The caret negates only when it is the first symbol after[; elsewhere (e.g.[e^]) it is a literal caret.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 49
- What does
?mean in a regex? Give an example.Zero or one occurrence of the previous character or expression:colou?rmatches color and colour.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 50
- What is the difference between the Kleene
*and Kleene+?*means zero or more occurrences of the preceding character or expression;+means one or more.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 50
- Write a regex for the sheep language baa!, baaa!, baaaa!, …, and say why
ba*!fails.baa+!(orbaaa*!).ba*!would also match b! and ba!, since*allows zero as.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 50
- Why is an integer written
[0-9]+rather than[0-9]*?[0-9]*also matches the empty string.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 50
- What do
{3}and.mean in a regex?{3}: exactly 3 occurrences of the previous character or expression (ax{10}z)..: a wildcard matching any single character except newline.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 50
- Write a regex to find lines in which the word rose appears twice.
rose.*rose:.*matches any string of zero or more characters between them.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 50
- What are anchors? What do
^and$match?Special characters that tie a match to a place in the string:^matches the start of a line,$the end.^The dog\.$matches a line containing only “The dog.”.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, pp. 50–51
- What are the three uses of the caret
^?Start of line (anchor), negation as the first symbol inside square brackets, and a literal caret.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, pp. 50–51
- What do
\band\Bmatch? Will\b99\bmatch in “299” and in “$99”?\bis a word boundary,\Ba non-boundary, where a “word” is a run of letters, digits or underscores. It won’t match inside 299 (99 follows a digit), but will match $99 ($ is not a word character).- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 51
- What does it mean that anchors and
\bare zero-width?They match the empty string at a position without consuming characters, so the following characters remain available to match.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 51
- Why can’t
[catdog]find “cat or dog”, and what should you write?Brackets match a single character from the set. Use the disjunction (pipe) operator:cat|dog.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, pp. 51–52
- Why doesn’t
guppy|iesmatch both guppy and guppies, and how is it fixed?Sequences bind tighter than|, so it means guppy or ies. Use parentheses:gupp(y|ies).- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 52
- Why does
Column [0-9]+ *not match repeated column labels, and how do you fix it?The*applies only to the preceding space. Group the whole sequence:(Column [0-9]+ +)*.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 52
- Give the regex operator precedence hierarchy, from highest to lowest.Parentheses
()> counters* + ? {}> sequences and anchors > disjunction|. Sothe*matches theeee (not thethe), andthe|anymatches the or any.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 52
- What does it mean that regex matching is greedy, and how do you get non-greedy matching?Patterns match the largest string they can (
[a-z]*on “once upon” matches once). The non-greedy operators*?and+?match as little as possible.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 52
- Refine a regex for the article the step by step.
themisses sentence-initial The →[tT]he, which wrongly matches inside other or there →\b[tT]he\b.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, pp. 52–53
- What are false positives and false negatives in pattern matching, and which measures do they correspond to?False positives are strings incorrectly matched (other); false negatives are strings incorrectly missed (The). Minimizing false positives increases precision; minimizing false negatives increases recall. The two efforts pull against each other.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 53
- What do
\d,\wand\s(and their capitals) match?\da digit,\wan alphanumeric character or underscore,\swhitespace.\D,\W,\Sare their complements.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 53
- How do you match a literal period, star or bracket?Escape it with a backslash:
\.,\*,\[,\\.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 53
- What are capture groups? Use one to convert US dates (mm/dd/yyyy) to dd-mm-yyyy.Parentheses store what they matched in numbered groups that can be referred to as
\1,\2…re.sub(r"(\d{2})/(\d{2})/(\d{4})", r"\2-\1-\3", s)turns 10/15/2011 into 15-10-2011.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 54
- Write a regex that finds a repeated word, like “the the”.
\b([A-Za-z]+)\s+\1\b: capture a word, then require the same text again after whitespace.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 54
- What is a non-capturing group, and when is it used?
(?: pattern ): groups for operator precedence without storing the match, e.g. to repeat a long sub-pattern 14 times but capture only the 15th occurrence.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 54
- How does ELIZA use regex substitutions?A cascade of substitutions on the uppercased input: first swap pronouns (MY → YOUR, I’M → YOU ARE), then match patterns and rewrite the whole response, e.g.
.* YOU ARE (DEPRESSED|SAD) .*→ “I AM SORRY TO HEAR YOU ARE \1”.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 55
- What are lookahead assertions?Zero-width tests on what comes next:
(?= pattern)succeeds if the pattern matches,(?! pattern)if it doesn’t, without advancing the match pointer. Negative lookahead is used to rule out special cases.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 55
- Write a regex capturing the first word of a line only if it doesn’t start with T or t.
^(?![tT])(\w+)\b- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 55
- What does the GPT-2 pretokenizer regex split “We’re 350 dogs! Um, lunch?” into?
We·’re·␣350·␣dogs·!·␣Um·,·␣lunch·?: contractions split off, words/numbers/punctuation runs separated, leading spaces attached.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, pp. 55–56
- What do
\p{L}and\p{N}match, and in which Python library?Any Unicode letter and any Unicode number (with\P{…}for the complements), in the externalregexlibrary, which is more powerful than the built-inre.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 2, §2.6, p. 56
Source
- Daniel Jurafsky and James H. Martin. Speech and Language Processing. 3rd edition (draft). https://web.stanford.edu/~jurafsky/slp3/