Regex Golf
Regex Golf
[Caption at top of panel:] Regex golf: [Megan is sitting at a laptop. Cueball is standing behind her.] Megan: You try to match one group but not the other. Megan: /m | [tn]|b/ matches Star Wars subtitles but not Star Trek . Cueball: Cool.
[Caption at top of panel:] Meta-regex golf: [A close-up of Megan at her laptop.] Megan: So I wrote a program that plays regex golf with arbitrary lists... Cueball (offscreen): Uh oh...
[Caption at top of panel:] Meta-meta-regex golf: [Megan typing at her laptop.] Megan: ...But I lost my code, so I'm grepping for files that look like regex golf solvers. [Cueball facepalming.]
[Caption at top of panel:] ...And beyond: [Another closeup of Megan at her laptop.] Megan: Really, this is all /(meta-)*regex golf/. Cueball: Now you have infinite problems. Megan: No, I had those already.

The comic talks about regular expressions , which are a way to specify textual patterns. Given a regular expression, one can search for the pattern it specifies inside a text string. If the pattern is found, it's said that the pattern "matches" the string; if it's not found, it's said it doesn't match.
The title of the comic and the first panel is based on " regex golf ", which is a discipline of " code golf ", a game in which programmers attempt to solve a given programming problem using as few characters as possible, analogous to the number of golf shots it takes to reach the goal. In regex golfing, the programmer is given two sets of text fragments, and tries to write the shortest possible regular expression which would match all elements of one set, while at the same time not matching any element from the other set.
The regex golf challenge Megan faces consists of matching all subtitles of (then extant) Star Wars films, while not matching any subtitle of Star Trek movies. Subtitles are the secondary titles of the movies, after the "Star Trek: " or "Star Wars Episode N: " . For example, in Star Wars Episode I: The Phantom Menace , the subtitle is The Phantom Menace . In the first panel, she created a 12-character regex solving the challenge.
Then she moved on to building a tool which would automatically build such a regex for arbitrary lists of text, which could be described as meta - regex golfing. But as she has lost this tool, she needs to search through her files and chooses a tool called " grep " to find it. This implies that she needs a regular expression that would find any code that appears to be a regex golf generator, which leads to another "meta-" layer of abstraction. At the end, Megan notes this sequence of meta-meta-... might go to infinity and Cueball quips that she now has "infinite problems" as a result of her efforts; Megan retorts that she already had "infinite problems" because she's geeky enough to run meta-versions of programs on themselves, and stubborn enough to continue on until she fails, to the exclusion of all else. This also seems to be a reference to a famous quote by Jamie Zawinski (see also 1171: Perl Problems ):
The first regex Megan uses is /m | [tn]|b/ , said to match Star Wars subtitles but not Star Trek .
The forward slashes / just mark the start and end of the regex. The | character means "or", so the regex matches any string that contains the patterns " m ", " [tn] " or " b " (including the spaces). The square brackets match one of the enclosed characters, meaning that " [tn] " matches either " t " or " n ". The regex is apparently case-insensitive, because it wouldn't work otherwise.
The Star Wars subtitles match the parts of the regex in the following way:
Note that if the animated film "Star Wars: The Clone Wars" were included, it would not be matched by " [tn] " because the T is the start of the subtitle and is not preceded by a space. None of the "Star Wars:" films titles announced since this comic ("The Force Awakens", "The Last Jedi", and "The Rise of Skywalker") match this regex.
On the other hand, none of the Star Trek subtitles contains an M followed by a space, a T or an N preceded by a space, or any B, so the regex does not match any of them. Note that in the original series all subtitles start with a "T" but it's the first character so it's not preceded by a space.
Here is the list that Megan probably used:
"Star Trek Beyond", which was released after this comic, would incorrectly match the regex since it is the first "Star Trek" title to contain a "b". However, since "Star Trek Into Darkness" and "Star Trek Beyond" both lack a colon in their titles, it is debatable whether they can truly be considered to have subtitles.
In the last panel "and beyond" Megan uses the regular expression /(meta-)*regex golf/ to describe her problem. * means "zero or more" of the preceding character/group (parentheses () group characters). So this regex matches "regex golf", "meta-regex golf", "meta-meta-regex golf", etc. In a way this is regex golf in itself, matching all levels of meta-regex golf while not matching anything else.
In the title text, there is a long regex that is the solution of another regex golf challenge: matching the last names of all elected US presidents but not their opponents. Note that the list of opponents include some people who were previously or later became presidents, or whose last name matches that of another person who was president, so taken literally this is impossible. To make this work the list of opponents must exclude any names of presidents. The regular expression itself works in a very similar way to the Star Wars/Trek one, including several different patterns separated by | . Each elected president matches one pattern while each opponent matches none.
The regex does not match either of the presidents elected since the comic’s release ("Trump" and "Biden"), and thus would need to be updated. The regex does match Hillary Clinton's last name, but because a person with the same last name (Bill Clinton) was president, this does not count as a mistake. There was already a losing opponent called George Clinton who ran in 1792 and 1812.
Here is a list of elected president and the patterns they match:
Some presidents are missing because they weren't elected but became presidents after the resignation/death of their formers.
And here is a list of how many unique last names are matched by each expression:
Randall's regular expression does not match presidential opponents Pinckney, King, Clay, Cass, Scott, Douglas, McClellan, Seymour, Greeley, Tilden, Hancock, Blaine, Bryan, Parker, Hughes, Cox, Davis, Smith, Landon, Willkie, Dewey, Stevenson, Goldwater, Humphrey, McGovern, Mondale, Dukakis, Dole, Gore, Kerry, McCain, or Romney.  However, it must be modified slightly, because it does match John C. Fremo nt , the runner-up to James Buchanan in 1856, as discussed by Peter Norvig at xkcd 1313: Regex Golf .  It also matches Aaron Bu rr , the runner-up to Thomas Jefferson in 1800.  Note that Norvig provides a small amount of Python code which actually plays regex golf with arbitrary lists, and found a shorter solution than Randall's for the Star Wars vs Star Trek game ( / t|p.*e/ ).
[Caption at top of panel:] Regex golf: [Megan is sitting at a laptop. Cueball is standing behind her.] Megan: You try to match one group but not the other. Megan: /m | [tn]|b/ matches Star Wars subtitles but not Star Trek . Cueball: Cool.
[Caption at top of panel:] Meta-regex golf: [A close-up of Megan at her laptop.] Megan: So I wrote a program that plays regex golf with arbitrary lists... Cueball (offscreen): Uh oh...
[Caption at top of panel:] Meta-meta-regex golf: [Megan typing at her laptop.] Megan: ...But I lost my code, so I'm grepping for files that look like regex golf solvers. [Cueball facepalming.]
[Caption at top of panel:] ...And beyond: [Another closeup of Megan at her laptop.] Megan: Really, this is all /(meta-)*regex golf/. Cueball: Now you have infinite problems. Megan: No, I had those already.
