Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [regular-expression]

Code challenges that involve the use of regular expressions.

6
votes
1answer
166 views

Insert commas into numbers [closed]

Write a program that reads a string, and inserts commas to divide large numbers in groups of 3. i.e. 10000 => 10,000. It should 'fix' incorrectly formatted numbers as described below. It must work ...
-3
votes
1answer
156 views

Regex only base64 decoder [closed]

Write a regular expression substitution string which can base64 decode a string using only a regular expression engine built into the language. Eg: ...
-2
votes
3answers
256 views

FreeChat Online [closed]

FreeChat Online It is 1985, and you are a humble Russian potato farmer. Sadly, your glorious homeland is going through a time of crisis and it needs your help! The KGB, with the "assistance" of your ...
16
votes
6answers
881 views

The Binary Fences

Input: An integer n in the range 2 <= n <= 10 A list of positive integers Output: Convert the integers to their binary ...
1
vote
2answers
456 views

Regular Expression to find a pangram [closed]

Awhile back I encountered a simple programming puzzle to determine whether a string was a pangram, or a perfect pangram. (Although it's worth noting that this particular challenge gave a different ...
11
votes
0answers
433 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 <...
14
votes
1answer
534 views

Regex for multiples of 9

It is easy to describe a finite state machine that recognizes multiples of 9: keep track of the digit sum (mod 9) and add whatever digit is accepted next. Such a FSM has only 9 states, very simple! By ...
6
votes
3answers
188 views

Create a regex to match a given range of integers in a given radix

Task Write a program/function that, given three integers n,a,b prints a regular expression which matches all of the base-n integers from ...
27
votes
3answers
3k views

A regex to match three consecutive integers iff the third integer is the sum of the first two

Write a regular expression which matches a given string consisting of three non-negative, space-separated integers if and only if the last integer is the sum of the previous two. Answers may be for ...
18
votes
2answers
530 views

Regex: Match an egalitarian series

Introduction I don't see many regex challenges on here, so I would like to offer up this deceptively simple one which can be done in a number of ways using a number of regex flavours. I hope it ...
6
votes
6answers
526 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 ...
3
votes
4answers
407 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 ...
82
votes
11answers
18k views

Regex, Paper, Scissors, Lizard, Spock

Warm up: Regex, Paper, Scissors This is the challenge I originally wanted to post, before realising that some very short solution exist. Nevertheless, it can be an interesting problem to think about ...
5
votes
1answer
1k 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 ...
7
votes
1answer
427 views

Regex that matches everything but itself

You must make a regex that matches everything except itself. For example, PPCG must not match PPCG, but must match everything ...
20
votes
41answers
3k 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: ...
40
votes
51answers
6k 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 ...
2
votes
0answers
128 views

Find letter-similar words in a word list [closed]

(I am not sure that this is the best place to put this question, but I will give it a shot. Forgive me if it is improper. This is just a fun project with which I am tinkering and I am looking for ...
19
votes
1answer
523 views

Make a regex polyglot

Write a regex that works in at least 2 regex flavors (or versions), and match a different string in each flavor (or version) it is running in. The strings to be matched in this challenge are the ...
14
votes
9answers
2k views

The codegolf object

Imagine this, we have an environment with a global scope containing just a single object, called codegolf. This object has a single child called ...
15
votes
2answers
784 views

Match the permutations!

Your challenge is to create a regex that matches every string permutation of itself, and nothing else. The match must also be case-sensitive. So, for example, if your regex is: ...
-2
votes
1answer
428 views

A regex to satisfy the grammar police (there vs. their vs they're)

We've all been burned by the grammar police on the internet. What we need is a regex that can be used in place of "there", "their", and "they're". This would shift burden of grammar parsing from the ...
15
votes
4answers
884 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 ...
37
votes
3answers
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, ...
6
votes
6answers
820 views

Is case sensitivity important? Part II: reality check

In this question Tom learned that, in general, there are many motivations to choose to include case sensitivity in his programming language, because the possible combinations for a variable name are ...
2
votes
2answers
269 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 ...
20
votes
5answers
711 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 ...
10
votes
3answers
323 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 ...
2
votes
1answer
271 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 ...
9
votes
2answers
301 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 ...
64
votes
7answers
5k 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 ...
13
votes
9answers
1k 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 ...
21
votes
2answers
460 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 ...
-3
votes
1answer
233 views

A regexp that does not match itself [closed]

Can you think of a regular expression that does not match itself?
27
votes
4answers
580 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
0answers
77 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. ...
15
votes
2answers
605 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 ...
12
votes
3answers
5k 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 ...
19
votes
8answers
10k 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 ...
3
votes
4answers
387 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" ...
28
votes
1answer
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: ...
39
votes
5answers
9k 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 ...
42
votes
16answers
5k 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 ...
-3
votes
1answer
129 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, ...
161
votes
57answers
38k 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 ...
18
votes
9answers
958 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 ...
25
votes
2answers
833 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 ...
-2
votes
1answer
251 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....
42
votes
42answers
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 ...
9
votes
2answers
514 views

Python: I wish I was PHP [closed]

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 ...