Questions tagged [regular-expression]

Code challenges that involve the use of regular expressions.

Filter by
Sorted by
Tagged with
32 votes
7 answers
5k 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: ...
32 votes
4 answers
6k 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 ...
19 votes
2 answers
1k 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 ...
  • 10.1k
19 votes
3 answers
4k 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 ...
  • 71.2k
371 votes
1 answer
116k 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, ...
50 votes
17 answers
9k 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 ...
42 votes
5 answers
19k 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 ...
160 votes
57 answers
42k views

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

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 ...
  • 2,313
82 votes
6 answers
40k 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 (...
  • 2,713
75 votes
40 answers
11k views

Cops and Robbers: Reverse 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 ...
65 votes
23 answers
15k 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"): ...
  • 5,811
41 votes
51 answers
7k views

Cops: Make a regex - Make a snake

This is the cop's thread. The robber's thread is here. Write a code that takes an input n and creates a n-by-n "snake matrix". A snake matrix is a matrix that ...
38 votes
10 answers
4k 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 ...
16 votes
4 answers
954 views

Match the Stack Exchange URLs

Prologue After installing an anti-XSS browser extension, Stack Snippets suddenly stopped working all across the Stack Exchange network. I could no longer learn from Stack Overflow, see working demos ...
  • 2,369
42 votes
42 answers
5k 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 ...
37 votes
3 answers
1k views

HexaRegex: A Tribute to Martin Ender

Martin Ender recently hit 100K, and has come up with some pretty awesome languages. We're going to have a bit of fun with one of them, Hexagony (and a bit of regex for Retina) As a brief overview, ...
35 votes
1 answer
2k views

Complement a POSIX Extended Regular Expression

Given a POSIX Extended Regular Expression as input, output its complement (also a POSIX ERE). If the given regular expression matches a string, its complement should not match it, and the regex: ...
  • 21.1k
28 votes
1 answer
1k 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: ...
  • 2,343
25 votes
2 answers
981 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 ...
  • 7,637
22 votes
41 answers
4k views

Robbers: Crack the regex - Make a snake

This is the robber's thread. The cop's thread is here. A snake matrix is a square matrix that follows this pattern: 3-by-3: 1 2 3 6 5 4 7 8 9 and 4-by-4: ...
22 votes
13 answers
2k views

Check if simple regex matches string

A simple regex is either: _ (which matches the empty string) Any lowercase letter a to z (...
user avatar
21 votes
11 answers
1k 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: ...
  • 802
20 votes
8 answers
15k 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 taken as any form of input, but ...
19 votes
2 answers
2k views

Complement of a Regex

For the purposes of this challenge, we define a postfix regex dialect with alphabet {1, 0} and the following operations: 1 and <...
18 votes
9 answers
1k 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 ...
16 votes
2 answers
758 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 ...
  • 439
15 votes
3 answers
4k views

Self-matching regex [closed]

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 ...
11 votes
4 answers
693 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 ...
  • 751
8 votes
6 answers
631 views

The greedy regex (cops)

This is the cops' post. The robbers' is here. You make a program and a regex. For most inputs, one of the following happens: the regex doesn't match, and the program returns a falsey value the ...
user avatar
7 votes
1 answer
2k views

Regular expression parser

This task is a classic programming task: Implement a regular expression parser. That is, write a program or function which takes a string describing a regular expression and a candidate string, and ...
  • 4,670
6 votes
4 answers
3k views

Count regex matches

Your task Given a simple regular expression, you have to count how many strings of length n have a match of length n with the ...
  • 13.9k
5 votes
3 answers
394 views

Negative Regexp [closed]

Write the shortest function that takes one string, and returns a regular expression that matches anything EXCEPT the input string, in parenthesis. Escape necessary characters like Perl ...
  • 5,929
4 votes
4 answers
490 views

The greedy regex (robbers)

This is the robbers' post. The cops' is here. Your challenge is to take a cop's post and find an input that the regex matches but the program returns a falsey value. Once you have cracked a cop's ...
user avatar