Code challenges that involve the use of regular expressions.

learn more… | top users | synonyms

11
votes
2answers
760 views

Compile Regexes

In this task you have to write a program that reads a regular expression and generates another program that outputs whether an input string is accepted by that regular expression. The output must be a ...
19
votes
4answers
426 views

Shortest simple regex matching a binary word

Task Define a simple regex as a non-empty regular expression consisting only of characters 0 and 1, grouping parentheses ( and ), one-or-more repetition quantifier +. Given a non-empty string of ...
1
vote
2answers
142 views

Check if a pattern appears in a DNA sequence

In a project I am working on, I am checking to see if certain DNA sequences appear in certain genes in E. coli. I have written a program in Java that performs the below features. However, since I ...
145
votes
56answers
33k views

Debunking Stroustrup's debunking of the myth “C++ is for large, complicated, programs only”

Stroustrup has recently posted a series of posts debunking popular myths about C++. The fifth myth is: “C++ is for large, complicated, programs only”. To debunk it, he wrote a simple C++ program ...
10
votes
4answers
539 views

Regex for Finding Radioactive Elements

Find the shortest regex that matches all radioactive elements and nothing else in the Periodic Table of Elements. Radioactive Elements 'Technetium','Promethium','Polonium','Astatine','Radon','...
74
votes
1answer
5k views

Regex that only matches itself

There are some pretty cool challenges out there involving regex (Self-matching regex, Regex validating regex) This may well be impossible, but is there a regex that will ONLY match itself? NOTE, ...
10
votes
1answer
187 views

RegEx-golf: match all contents in a string

Your task is to write a RegEx that matches everything inside strings. A string is defined as everything surrounded by (but not including) two unescaped ". A " can be escaped by \, which can also be ...
2
votes
1answer
210 views

Find the last three words of each paragraph if they are shorter than 20 characters in total

Based on my SO question, but more specifically: Find a RegEx such that, given a text where paragraphs end with an empty line (or $ at the very end), match the up to twenty last characters of a ...
9
votes
2answers
236 views

Inverse regex of compound interest

Koronkorko is the Finnish word for compound interest. We don't want compound interest in our strings, so let's find the shortest possible regular expression to exclude it. Given a string consisting ...
27
votes
3answers
944 views

Gotta Match Them All! Write a regex to match the original 151 Pokemon but no subsequent Pokemon

The original 151 Pokemon, in an array format suitable for initializers: ["Bulbasaur", "Ivysaur", "Venusaur", "Charmander", "Charmeleon", "Charizard", "Squirtle", "Wartortle", "Blastoise", "Caterpie", ...
57
votes
7answers
4k views

Regex Golf: Verify a Sudoku Solution

Write a regex which matches any valid sudoku solution and doesn't match any invalid sudoku solution. The input is an unrolled version of the sudoku, i.e. there are no line delimiters. E.g. the ...
31
votes
16answers
4k views

Tips for Regex Golf

Similar to our threads for language-specific golfing tips: what are general tricks to shorten regular expressions? I can see three uses of regex when it comes to golfing: classic regex golf ("here ...
13
votes
2answers
470 views

ValiDate ISO 8601 by RX

Challenge Find the shortest regex that validates, i.e. matches, every possible date in the Proleptic Gregorian calendar (which also applies to all dates before its first adoption in 1582) and does ...
65
votes
7answers
24k views

Hard code golf: Regex for divisibility by 7

Matthias Goergens has a 25,604-character (down from the original 63,993-character) regex to match numbers divisible by 7, but that includes a lot of fluff: redundant parentheses, distribution (xx|xy|...
13
votes
9answers
993 views

Quinean Regex Tester

This challenge is pretty simple. As input, you take in a regular expression. Then, you output a truthy/falsey of whether or not your source code matches the regular expression. It's that simple! Just ...
23
votes
4answers
3k views

Match strings whose length is a fourth power

Within the scope of this question, let us consider only strings which consist of the character x repeated arbitrary number of times. For example: <empty> x xx xxxxxxxxxxxxxxxx (Well, ...
9
votes
2answers
447 views

Python: I wish I was PHP

There's an old story about a stonecutter who was never satisfied with what he was. He wished he could become the sun, and so he was. Blocked by the clouds, he wished to be - and became - a cloud. When ...
20
votes
2answers
422 views

Compile Regexes (By Substitution)

Your task is to compile regexes... by specifying a substitution for each character in a regex. Regexes The regexes support these REGEX = (LITERAL REGEX / GROUP REGEX / STAR REGEX / ...
27
votes
4answers
424 views

Reverse a regex

The Challenge Given a valid regex, output a regex that that matches the same set of strings, but reversed. The Task This challenge uses the most basic regex operations: ^, $, ?, +, *, [], {}, |. ...
-2
votes
1answer
133 views

A regexp that does not match itself [closed]

Can you think of a regular expression that does not match itself?
1
vote
0answers
69 views

Simple regex parser [duplicate]

Let's parse some regex. Not quite the regex you are used to, but the formal regular expressions taught in courses on automata theory. Wikipedia knows what I'm talking about. We have four operations. ...
24
votes
4answers
4k views

Meta regex golf

In the spirit of this xkcd Write a program that plays regex golf with arbitrary pairs of lists. The program should at least attempt to make the regex short, a program that just outputs /^(item1|...
12
votes
3answers
2k views

Self-matching regex

Write a non-trivial regex that matches itself. For example, #.*$ will match a comment outside of a string in python until the end-of-line, and also matches itself in perl regex syntax. Rules: The ...
69
votes
39answers
8k views

Cops and Robbers: Regex Golf

Note: This challenge is now closed. Any future cops' submission will not be considered for the accepted answer. This is to ensure that no one can post a very simple regex in the future that only ...
12
votes
3answers
1k views

Is that word Feminine or Masculine?

Write a program or function that takes in a single string containing only lowercase a-z, and prints or returns a truthy value if the word is the feminine version of the thing it represents and a falsy ...
39
votes
42answers
4k views

A Different Kind of Meta Regex Golf

Notice: Following popular demand I have slightly relaxed the rules: The maximum regex size grows by 1 byte every 5 answers. Answer N may use up to 29 + ⌈N/5⌉ bytes. The score of each answer ...
13
votes
8answers
2k views

Remove single line and multiline comments from string

Goal Using the programming language of your choice, write the shortest program to eliminate comments from a string representing a C program. Input The string can be... given as a function ...
3
votes
4answers
346 views

Words of centuries

I'd like to get a list of words that are strongly connected with a particular time epoch, for example, 19th century, 20th century and 21st century. I suspect that nobody used a word like "smartphone" ...
39
votes
5answers
4k views

Adding Numbers with Regex

I want to try a new type of regex golf challenge, which asks you to solve nontrivial computational tasks with nothing but regex substitution. To make this more possible and less of a chore, you will ...
-3
votes
1answer
114 views

Listing links from pages — with error handling

In my previous question Debunking Stroustrup's debunking of the myth “C++ is for large, complicated, programs only”, a lot of people complained that comparison with Stroustrup's program isn't fair, ...
15
votes
4answers
21k views

Shortest URL regex match in JavaScript

Create the shortest regular expression that will roughly match a URL in text when run in JavaScript Example: "some text exampley.com".match(/your regular expression goes here/); The regular ...
24
votes
2answers
633 views

Partial Ordering of Regex Patterns

For the purpose of this challenge, we say that a regex pattern matches a string if the entire string is matched by the pattern, not just a substring. Given two regex patterns A and B, we say that A ...
18
votes
9answers
909 views

Sum the time durations

Challenge Write the shortest code that can sum all the time durations that appear in the stdin. The program must only consider the strings that match with one of the following patterns and ignore the ...
-2
votes
1answer
157 views

Algorithm for finding a regex for a list of “words” [closed]

I just discovered this website and was amazed. Somehow this challenge popped into my mind: Provided a very long list of words; Write or describe an algorithm which finds a regex to match them all. Ex....
14
votes
2answers
1k views

Regex in reverse - decompose regular expressions

The Problem I have a bunch of regular expressions that I need to use in some code, but I'm using a programming language that doesn't support regex! Luckily, I know that the test string will have a ...
15
votes
12answers
1k views

Implement glob Matcher

Implement a function of pattern and string to be matched, return true if the pattern matches the WHOLE string, otherwise false. Our glob pattern syntax is: ? matches any one character + matches one ...
13
votes
6answers
2k views

Interpret /// (pronounced 'slashes')

Because we can't get enough of esoteric language golfs, can we? ///—pronounced slashes—is a fun little language based on the s/// regex-replacement function of Perl fame. It contains only ...
10
votes
1answer
989 views

Implement PCRE in your language.

Note: After trying this myself, I soon realized what a mistake this was. Therfore, I am modifying the rules a bit. The minimum required functionality: Character classes (., \w, \W, etc.) ...
3
votes
1answer
2k views

Match strings whose length is isolated prime

Isolated prime definition from Wikipedia: An isolated prime is a prime number p such that neither p − 2 nor p + 2 is prime. In other words, p is not part of a twin prime pair. For example, 23 is ...
6
votes
1answer
562 views

Regex that matches any regex [duplicate]

Your task is to write a regular expression that matches any valid regular expression and no invalid expression. You may pick any regex flavour for your answer, but you have to match regular ...
10
votes
0answers
481 views

Complement a Regular Expression

Given a regular expression as input, output its complement. If the given regular expression matches a string, its complement should not match it, and the regex: (regex given)|(complement) should match ...
-1
votes
1answer
145 views

POSIX reg. exp to test the divisibility by 9

Write the shortest POSIX-form regular expression that matches only non-negative integers divisible by 9. The non-negative integer should be given in base 10.
21
votes
11answers
976 views

Cross-matching regular expressions

Your task here is to write two regular expressions, each of which matches the other one but does not match itself. Both regular expressions should have this form: /pattern/optional-flags This is ...
3
votes
1answer
395 views

Convert Regexes

Suppose you have existing regexes you wish to compile using one of the submissions to Compile Regexes. In as few characters as possible, write a program that: accepts an existing regex as input, as ...
22
votes
5answers
1k views

Regex Golf: Regions of Italy vs. States of USA

We've already got a meta-regex-golf problem as inspired by the xkcd comic But, this regex golf looks fun, too! I want to distinguish between the states of the US and the regions of Italy. Why? I'...
3
votes
2answers
166 views

Reverse Regular expressions [closed]

The idea here is to write a little segment that will spit out regular expression patterns when given a string that matches them. As you might have guessed, this will be a popularity contest, and I'll ...
3
votes
3answers
375 views

Regex for Finding Non-Radioactive Elements

Find the shortest regex that matches all non-radioactive elements and nothing else in the Periodic Table of Elements. This is the inverse of Regex for Finding Radioactive Elements Radioactive ...
8
votes
4answers
884 views

Generate regular expressions for matching natural numbers between `m` and `n`

The type of regular expression is PCRE. Write a program that output a valid PCRE such that it matches all natural numbers between m and n and does not match anything else. No leading zeros are ...
16
votes
2answers
500 views

Singular or plural?

Here is a list of 30 English nouns in their singular and plural form. Your job is simply to write the shortest regular expression that matches all the nouns in one form and none in the other (easy, ...
55
votes
24answers
12k views

Shortest unmatchable regular expression

Your mission is to write the shortest valid regular expression that no string can match, empty string included. Submissions must have this form ("literal notation"): /pattern/optional-flags ...