
Regex Golf is a programming game where the goal is to write a short and accurate regular expression (regex) code. The game involves matching all items in one list without matching any item in a second list. Each correct match adds points, while each incorrect match or character in the regex subtracts points. The game has multiple levels, with the first level requiring simple text matching and subsequent levels introducing more complex features. Players can access solutions and leaderboards online, and the game has sparked discussions on forums such as Reddit, Stack Exchange, and GitHub.
| Characteristics | Values |
|---|---|
| Goal | Write a regex that matches all items in one list without matching any item in a second list |
| Requirements | The regex has to be as short as possible |
| Scoring | Each correct match adds ten points, each incorrect match subtracts ten points, and each character in the regex subtracts one point |
| Level types | Warmup, Anchors, Backrefs, Abba |
| Tips | Use atomic groups and possessive quantifiers, use recursion and variable-length lookbehinds |
| Solutions | Use a regex golf solution generator, refer to best known solutions |
Explore related products
What You'll Learn
- Use atomic groups and possessive quantifiers to match a string and disallow backtracking
- Use recursion, variable-length lookbehinds, and character class combination operators
- Use double negatives for lookaheads with 3 or more constructs that always match
- Use a regex engine that dynamically updates based on 'rational' updates to initial attempts
- Use a regex golf solution generator to stare at puzzles and generated solutions

Use atomic groups and possessive quantifiers to match a string and disallow backtracking
When it comes to regex golf, atomic groups and possessive quantifiers can be a powerful tool to match a string and disallow backtracking. This technique is particularly useful when you need to prevent backtracking to find the correct match.
Let's start by understanding the basics of atomic groups and possessive quantifiers. Atomic groups are a way to group a set of characters in a regular expression, indicating that the entire group should be treated as a single, indivisible unit. On the other hand, possessive quantifiers are a type of quantifier that modify the behaviour of a regular expression pattern to disallow backtracking.
By using atomic groups and possessive quantifiers together, you can create more efficient and precise regular expressions. Atomic groups ensure that the grouped characters are matched as a whole, while possessive quantifiers prevent the regex engine from backtracking and trying alternative permutations. This combination is especially useful when you have nested quantifiers or when you want to match a specific string without allowing any backtracking.
For example, consider the regular expression ".*" which matches "abc" in "abc"x. By using a possessive quantifier, you can modify the expression to ".*+", and now it will
Additionally, atomic groups can be very useful in certain situations. For instance, when you cannot use a negated character class to prevent a repeated regex token from matching the non-repeated regex token that follows it. In such cases, atomic groups ensure that the repeated token is matched as a whole, preventing unintended matches.
In summary, atomic groups and possessive quantifiers are powerful tools in regex golf that can help you match strings and disallow backtracking. They provide more control over the matching process and can prevent issues like catastrophic backtracking. By understanding how to use these tools effectively, you can write more efficient and precise regular expressions.
Golf Shots: What's Your Score?
You may want to see also
Explore related products

Use recursion, variable-length lookbehinds, and character class combination operators
The regex module is intended to replace the re module and comes with several interesting features. It adds support for recursion, variable-length lookbehinds, and character class combination operators. It also has the unique feature of fuzzy matching. This means that you can specify a number of errors (insertions, deletions, substitutions) that are allowed, and the engine will provide you with approximate matches.
Recursion can save a lot of bytes in more complicated patterns. There is no need to make a call to another group inside that group itself. If a certain pattern appears several times in your regex, group it and refer to it outside that group. This is similar to a subroutine call in normal programming languages. Note that this is not the same as a backreference (\1). Backreferences match the exact same string that the group matched the last time.
Variable-length lookbehinds allow you to look back to the start of the string by using .* and then beginning a lookahead, such as (?<=(?=lookahead).*).
Predefined character classes can be used to save bytes. For example, \d matches a decimal digit, which is three bytes shorter than [0-9]. Most challenges won't be affected by the slight difference that \d may also match Unicode digits in some flavours.
Other tips for regex golf include using atomic groups ((?>...)) and possessive quantifiers (?+, *+, ++, {m,n}+). These match a string and disallow backtracking later. So, they will only match the first matchable string found by the regex engine.
Golf and Wolf: A Perfect Rhyme Pair
You may want to see also
Explore related products

Use double negatives for lookaheads with 3 or more constructs that always match
Lookaheads are an important concept in regular expressions (regex), and they can be used to match elements before or after a certain match. There are two types of lookaheads: positive and negative.
Positive lookaheads are used to match a particular element, character, or group after the item matched. For example, if you want to match all cars that have a status of "sold" in a list, you would use a positive lookahead.
Negative lookaheads, on the other hand, are used to match items that are not immediately followed by a certain character, group of characters, or regex group. For example, if you want to match all cars that don't have a status of "sold", you would use a negative lookahead.
Now, let's discuss the strategy of using double negatives for lookaheads with 3 or more constructs that always match. In certain situations, using double negatives can be beneficial. For instance, if you have multiple lookahead constructs that always match, using a double negative can help capture subexpressions effectively. This technique is particularly useful when dealing with complex relationships and patterns in your data.
Here's an example to illustrate this concept:
> \d+(?=_(?!_))
In this example, the lookahead is used to match a number that is followed by one underscore but not more. The outer lookahead asserts that what follows the current position is one underscore. Then, the inner negative lookahead (?!_) asserts that what follows the underscore is not another underscore. This is a more concise and elegant approach compared to other variations.
By using double negatives in this way, you can efficiently capture specific patterns and relationships in your data, making it a powerful tool in your regex arsenal.
Hybrid Golf Clubs: How Far Can You Go?
You may want to see also
Explore related products

Use a regex engine that dynamically updates based on 'rational' updates to initial attempts
A regular expression, or regex, is a sequence of characters that specifies a match pattern in text. A regex engine is a program that takes an input and a pattern and determines if they match.
A fast regex engine can be built with dynamic programming, which involves using recursion to support the dot (.) wildcard. However, for more complex regex patterns, the algorithm deteriorates into a brute-force/backtracking algorithm. This is where the concept of dynamic updates comes in.
To build a customised regex engine that dynamically updates based on rational updates to initial attempts, you can use atomic groups and possessive quantifiers. This matches a string and disallows backtracking later, only matching the first matchable string found by the regex engine. For example, to match a string with an odd number of a's at the beginning, which is not followed by more a's, you can use atomic groups. This allows you to freely use .* and ensures that there won't be another possibility of matching too many or too few characters, which may break your pattern.
Another approach is to use a combination of pointers and recursion. By assigning pointers to the input and pattern strings, you can indicate where you are in the process. This allows you to make choices about matching single or multiple characters and recursively applying the algorithm.
Additionally, you can use features such as fuzzy matching, which allows you to specify a number of errors (insertions, deletions, substitutions) that are allowed, and the engine will provide approximate matches.
Golfing with Bone Spurs: How to Adapt Your Swing
You may want to see also
Explore related products

Use a regex golf solution generator to stare at puzzles and generated solutions
Regex Golf is a programming game where the goal is to write a regex that matches all the items in one list without matching any item in a second list. The catch is that the regex has to be as short as possible. The game is a descendant of code golf, an older programming game where the goal is to write the shortest possible program that performs a given task.
There are three main uses of regex in golfing: classic regex golf ("here is a list that should match, and here is a list that should fail"), using regex to solve computational problems, and regular expressions used as parts of larger golfed code.
There are several tricks and tips to shorten regular expressions and improve your score. For example, atomic groups and possessive quantifiers can be used to match a string and disallow backtracking later. This will only match the first matchable string found by the regex engine. In \.NET regex, you can use this to pop group 1 the greatest multiple of 3 (with a maximum of 30) times.
Another tip is to use an empty negative lookahead when using regex to solve computational problems or match highly non-regular languages. This involves making a branch of the pattern fail regardless of where you are in the string. A simpler option is to use a character that you know will never appear in the input. For example, if the input will always consist of digits, you can use a non-digit character.
Additionally, the regex module is intended to replace the re module and offers interesting features such as support for recursion, variable-length lookbehinds, and character class combination operators. It also has fuzzy matching, which allows you to specify a number of errors (insertions, deletions, substitutions) and provides approximate matches.
While there are no standard strategies for winning regex golf, using a regex golf solution generator and studying the puzzles and their generated solutions can help improve your skills and performance in the game.
Left-Handed Golfers: Play Your Natural Way
You may want to see also
Frequently asked questions
Regex Golf is a programming game where the goal is to write a regex that matches all the items in one list without matching any item in a second list. The regex has to be as short as possible.
Each correct match adds ten points, each incorrect match subtracts ten points, and each character in the regex subtracts one point.
Some tricks to shorten regular expressions include using atomic groups and possessive quantifiers, and utilising the unique features of the regex module, such as fuzzy matching.











































