I want to invite you to wrestle an evening or two over interesting puzzles, for regular expressions, which Callum Macrae puts
on its website on GitHub every Tuesday.
Each question is presented in the form of a set of tests. The task is to write such a regular expression so that all the tests turn green.
Some of the tasks themselves are quite simple, and the most interesting part is to write the shortest possible regular expression.
Tests use JavaScript Regex engine of your browser, which has all the basic features of PCRE. More details can be
found here (English) , in the column ECMA in the table.
')
I have collected in this article Russian versions of tasks and materials that can help in solving them. It would be interesting to see the most interesting solutions in the comments.
UPD: In regular expressions, ECMAScript does not have retrospective checks.
Task:
Select the tag
<strong> repeated words.
Examples:
his is a test
=> this is a test
his is is a test
=> this is <strong>is</strong> a test
Task:
Choose shades of gray in different color systems.
You can read about the colors
at this link .
Examples:
#FFF
- Yes
rgb(2.5, 2.5,2.5)
- Yes
rgb(2, 4, 7)
- No
Task:
Select existing dates between 1000 and 2012. Seconds can be omitted.
The author facilitates the task: each month has 30 days.
Examples:
2012/09/18 12:10
- Yes
2013/09/09 09:09
- No (after 2012)
Task:
Convert text framed as asterisks to italics. Do not touch the text in double asterisks (bold).
Read more about
MarkDown in Wikipedia .
Examples:
*this is italic*"
=> <em>this is italic</em>
**bold text (not italic)**
=> **bold text (not italic)**
Task:
Select numbers with a comma or a space, as a separator of discharges. (fortunately without mommaye)
Examples:
8,205,500.4672
- Yes
1,5826,000
- No
Task:
Select
IPv4 addresses in all possible representations: decimal, hexadecimal and octal. With points and without. More information about IP addresses can be found
in Wikipedia.Examples:
99.198.122.146
- Yes
0xFF.255.0377.0x12
- Yes
256.256.256.256
- No
Task:
Domain names for the
http and
https protocols, with an optional slash at the end. Special characters are not used.
Examples:
http://example.com/
- Yes
example.com
- No
.
- No : (
Task:
Find and highlight (bold) items in the
MarkDown list.
Examples:
* First list item
* Second list item
=>
* First list item
* Second list item
* Repeated list item
* Repeated list item
=>
* Repeated list item
* **Repeated list item**
Task:
Convert
MarkDown links to
HTML . They look like this:
[text](http://example.com)
The main thing is not to confuse with pictures

Examples:
[Basic link](http://example.com)
=> <a href="http://example.com">Basic link
[Invalid](javascript:alert())
=> [Invalid](javascript:alert())
Task:
Break the sentence into tokens. This can be useful, for example, for a search engine.
There are a few rules:
- A few words in quotes must fall into one token.
This "huge test" is pointless
=> this,huge test,is,pointless
- Words written with a hyphen also fall into one token.
Words written through several hyphens (dashes), or having a hyphen at the beginning or at the end fall into separate tokens.
Suzie Smith-Hopper test--hyphens
=> Suzie,Smith-Hopper,test,hyphens.
- Reductions (contractions) fall into one token
I can't do it
=> I,can't,do,it.
- All punctuation except apostrophes and hyphens must be removed.
Too long; didn't read
Too long; didn't read
=> Too,long,didn't,read.
Task:
Select a sequence of non-repeating characters in alphabetical order. Spaces should be ignored. Unfortunately, the solutions I know are not very successful.
Examples:
abcdefghijk
- Yes
abbc
- No
Task:
Remove duplicate spaces and tabs, leave one space between words and two each between sentences.
Examples:
Extra spaces
=> Extra spaces
Sentence. Sentence.
=> Sentence. Sentence.
Sentence. Sentence.
Task:
Choose duplicate words that are directly under each other.
The use of a monospace font is assumed. Lines longer than 32 characters are transferred.
Examples:
This sentence is pretty long and
this sentence is also a test
- Yes
This sentence also shouldn't
match as this has no words
below.
- No
Task:
> Select the first 50 chemical elements of
the periodic table . The solution is quite obvious, so the task is to find the shortest possible solution.
Examples:
H
- Yes
M
- No
Task:
Select music chords, such as
Cmin , or
Bmaj . Looking for a brief and complete record. For this task, we assume that the chords
E♯ ,
B♯ ,
F and
C ♭ do not exist.
For those interested, there is a
good article about chords in Russian and English article
on Wikipedia, where appropriate symbols are used.Also note that sharp (♯)
it's not the same as pound (#).
Examples:
C
- Yes
Z
- No
Task:
Choose chemical elements with an atomic number greater than 50.
Examples:
I
- Yes
A
- No
Task:
Choose a well-constructed regular expression. For starters, we restrict ourselves to literals (possibly shielded), classes, and several quantifiers.
Examples:
/regexp?/
- Yes
regex
- No
Task:
Choose a properly shaped IRC message.
Here is a link to the
Russian version of the specification.Examples:
[_]!abc@test PRIVMSG #chat :Test
- Yes
cm!callum@lynx.io PRIVMSG #chat :Hello!
- No