
Regex Golf is a programming game where the goal is to write a regular expression (regex) that matches all the items in one list without matching any item in a second list. The regex must also be as short as possible. The game is a derivative of code golf, a much older programming game where the goal is to write the shortest possible program that performs a given task. In regex golf, the player is given two sets of text fragments and must write the shortest possible regex that matches one set of fragments while not matching the other.
| Characteristics | Values |
|---|---|
| Type | Programming game |
| Goal | Write a regex that matches all items in one list but none in another list, with the shortest regex possible |
| Origin | First introduced by the website regex.alf.nu in 2013, but unnamed until 2014 when Randall Munroe published xkcd #1313: "Regex Golf" |
| Scoring | Each character in the regex costs one point. Correct matches earn 10 points, while incorrect matches lose 10 points. |
| Hard mode | Both lists are extended with additional items, making it harder to find a correct regex |
| Example challenge | Matching subtitles of Star Wars films but not Star Trek films |
Explore related products
What You'll Learn
- The game involves finding a regular expression that matches one set of strings while not matching another set
- The goal is to write the shortest possible regular expression
- There are different levels with varying degrees of difficulty
- Players can lose points for incorrect solutions or matching something they shouldn't
- It is a derivative of code golf, where the goal is to write the shortest program to perform a task

The game involves finding a regular expression that matches one set of strings while not matching another set
Regex Golf is a programming game that involves finding a regular expression that matches one set of strings while not matching another set. The game is derived from code golf, where programmers attempt to solve a given programming problem using the fewest number of characters, 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 must write a concise regular expression that matches all elements of one set while excluding elements from the other.
For example, in one instance of the game, the challenge was to create a regex that matched the subtitles of Star Wars films but not those of Star Trek films. The subtitles being the secondary titles after "Star Trek:" or "Star Wars Episode N". This required a regular expression that identified patterns in the text to differentiate between the two franchises.
The game can be quite intricate, with various levels of difficulty. Some levels have a hard mode, where additional items are included in both lists to prevent brute-force solutions, and incorrect solutions result in point deductions. Players are challenged to find the shortest possible regex that satisfies the given conditions.
To excel at regex golf, players can utilise different techniques and flavours, such as atomic groups, possessive quantifiers, recursion, and character class combination operators. The Perl flavour, for instance, allows calling Perl code from within the regex and supports recursion and subroutine features. The ECMAScript flavour, on the other hand, is considered limited for golfing purposes.
Overall, regex golf is a challenging and engaging pastime for programmers, requiring creativity and a deep understanding of regular expressions to craft concise solutions that match specific patterns while avoiding others.
Jason Day: Golf's Unsung Hero
You may want to see also
Explore related products

The goal is to write the shortest possible regular expression
Regex Golf is a discipline of code golf, a game in which programmers attempt to solve a given programming problem using as few characters as possible. In a typical game of regex golfing, the programmer is given two sets of text fragments and tries to write the shortest possible regular expression that matches all elements of one set while not matching any element from the other set.
For example, in the comic referenced, the two sets are the subtitles of the films from the Star Wars and Star Trek franchises. The goal is to write a regex that matches all Star Wars subtitles and none of the Star Trek subtitles. The challenge is to do this in as few characters as possible.
In another example, the task is to write a regex that matches the last names of all elected US presidents but not their opponents. This is a particularly tricky challenge because there is some overlap between the names of winners and losers, making it impossible to create a perfect regex.
The process of regex golfing can be complex and requires a good understanding of regular expressions and their syntax. Regular expressions are used to search for patterns in text, and the goal of regex golfing is to find the shortest possible expression that matches the desired pattern. This involves using various metacharacters, atoms, quantifiers, and other regex constructs to create concise and accurate patterns.
Overall, the goal of writing the shortest possible regular expression in regex golf challenges encourages programmers to be creative and efficient in their use of regular expressions, leading to the development of concise and elegant solutions to complex problems.
Tiger's Daughter: A Golfer in the Making?
You may want to see also
Explore related products

There are different levels with varying degrees of difficulty
Regex Golf is a discipline of "code golf", a game in which programmers attempt to solve a given programming problem using as few characters as possible. The name is analogous to the number of golf shots it takes to reach the goal. In a game of regex golfing, the programmer is given two sets of text fragments and tries to write the shortest possible regular expression (also known as a regex) that will match all the items on one list and none of the items on the other list.
Some players have expressed interest in proving that certain functions are impossible to perform in a regex. For instance, it is said to be impossible to create a perfect regex for matching email addresses. However, some players have come close to a solution, such as .*@.*.
Another challenge involves matching the subtitles of Star Wars films while excluding the subtitles of Star Trek films. This has proven to be difficult due to the changing nature of film titles over time, with new releases causing previous solutions to become obsolete.
There is also a level of meta-regex golfing, where the player creates a tool that automatically builds a regex for arbitrary lists of text, adding a layer of complexity to the game.
Golf: Clear Your Mind, Improve Your Game
You may want to see also
Explore related products

Players can lose points for incorrect solutions or matching something they shouldn't
In Regex Golf, players attempt to write a regex that matches all the items in one list without matching any item in a second list. The regex must also be as short as possible. The game is derived from code golf, where programmers attempt to solve a given programming problem using as few characters as possible.
Players can gain or lose points depending on their solutions. They get ten points per match but lose ten points if they match something they shouldn't. Each character used costs one point. For instance, in the Anchors level, players are deducted one point per character used and ten points if they match something they shouldn't.
The game has several levels, each with a normal and hard mode. The hard mode is more challenging as it includes additional items that are automatically generated, making it more difficult for players to come up with correct solutions. Incorrect solutions can result in a deduction of 100 points from the player's score.
An example of a challenge in the game involves matching subtitles of Star Wars films while not matching subtitles of Star Trek films. Another challenge involves matching the last names of all elected US presidents but not their opponents.
Overall, Regex Golf is a game that tests players' ability to write concise and accurate regular expressions to match specific patterns while avoiding others. Players must carefully construct their regex to maximize points earned while minimizing points lost.
Golf Drivers: Is Price Tag Worth the Buy?
You may want to see also
Explore related products

It is a derivative of code golf, where the goal is to write the shortest program to perform a task
Regex Golf is a derivative of Code Golf, a programming game where the objective is to write the shortest possible program to perform a specific task. In Regex Golf, the challenge is to create a regular expression (regex) that matches all the items in one list while excluding those in another list. The regex must be as concise as possible, adding complexity to the game.
The game was first introduced by the website regex.alf.nu in 2013, but it remained unnamed until Randall Munroe published xkcd #1313: "Regex Golf" on January 6, 2014. In the xkcd comic, the two sets of text fragments used in the challenge were the subtitles of films from the Star Wars and Star Trek franchises. The goal was to create a regex that matched all Star Wars subtitles while excluding any Star Trek subtitles. This example sparked interest in the concept, and it has since become a popular programming pastime.
The game has various levels, each with a normal and hard mode. In hard mode, the lists are extended with automatically generated items, making it more challenging to find the correct regex. Points are awarded or deducted based on the accuracy and length of the regex. Each character used in the regex costs a point, and matching an incorrect item results in a 10-point deduction. This scoring system encourages players to find the shortest possible regex that accurately distinguishes between the two sets of text fragments.
To excel at Regex Golf, players can utilise features such as atomic groups, possessive quantifiers, recursion, and variable-length lookbehinds. These tools allow for more efficient and concise regular expressions. Additionally, different flavours of regex, such as Perl, ECMAScript, and Lua, offer unique capabilities that can be leveraged for golfing.
Overall, Regex Golf is a challenging and engaging derivative of Code Golf, requiring creativity and a deep understanding of regular expressions to craft the shortest possible regex that accomplishes the given task.
Golf Buddy VS4: Updating Your GPS Device
You may want to see also
Frequently asked questions
Regex golf is a programming game where the goal is to write a regular expression (regex) that matches all the items in one list without matching any item in a second list. The regex must also be as short as possible.
A regular expression is a way of searching for patterns in text without knowing exactly what you're looking for.
In one challenge, the two sets are the subtitles of films from the Star Wars and Star Trek franchises. The regex must match all Star Wars subtitles and not match any Star Trek subtitle.
The regex /m | [tn]|b/ matches all Star Wars subtitles but not Star Trek subtitles.











































