📜 ⬆️ ⬇️

Mash the brain with regular expressions - Regex Tuesday Challenge

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.


1. Select the repeated words (Link leads to the task)


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

2. Grayscale


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

3. Dates to find strings matching this pattern: YYYY / MM / DD HH: MM (: SS)


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)

4. Italic in MarkDown


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)**

5. Numbers


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

6. IPv4 addresses


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

7. Domain Names


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 : (

8. Duplicate items in MarkDown list


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

9. Links to MarkDown


Task:
Convert MarkDown links to HTML . They look like this: [text](http://example.com)
The main thing is not to confuse with pictures ![alt text](image location)

Examples:
[Basic link](http://example.com) => <a href="http://example.com">Basic link
[Invalid](javascript:alert()) => [Invalid](javascript:alert())

10. We divide the sentence into tokens.


Task:
Break the sentence into tokens. This can be useful, for example, for a search engine.

There are a few rules:



11. Letters in alphabetical order.


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

12. Correcting spaces


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.

13. Duplicate words under each other


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

14. Brutforsim chemical elements


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

15. Musical chords


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

16. Brutforsim chemical elements


Task:
Choose chemical elements with an atomic number greater than 50.

Examples:
I - Yes
A - No

17. Regular expression for regular expression.


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

18. IRC - Messages


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

Source: https://habr.com/ru/post/167015/


All Articles