Kneading the brain with regular expressions - Regex Tuesday Challenge

    I want to suggest you break your head in the evening or two over interesting tasks, into the regular expressions that Callum Macrae puts on its website on GitHub every Tuesday.

    Each question is presented as a set of tests. The goal is to write such a regular expression so that all 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 the JavaScript Regex engine of your browser, which has all the basic features of PCRE. More details can be found here (in English) , in the ECMA column in the table.

    In this article I have collected Russian versions of tasks and materials that can help in their solution. It would be interesting to see the most interesting solutions in the comments.

    UPD: There are no retrospective checks in ECMAScript regular expressions.


    1. Highlight repeating words (Link leads to a task)


    Task:
    Tag <strong> duplicate 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.
    Read about 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 may be omitted.
    The author facilitates the task: 30 days in each month.

    Examples:
    2012/09/18 12:10- yes
    2013/09/09 09:09- No (after 2012)

    4. Italics in MarkDown


    Task:
    Convert text framed in asterisks to italics. Do not touch the text in double asterisks (bold).
    Read more about MarkDown can be 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 space, as a separator of digits. (fortunately there were no 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 and without dots. More information about IP addresses can be found on 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 MarkDown Items


    Task:
    Find and bold (**) repeating 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 in MarkDown


    Task:
    Convert MarkDown links to HTML . They look like this: [text](http://example.com)
    The main thing is not to be confused with the 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. Divide the offer into tokens.


    Task:
    Break offer into tokens. This may be useful, for example, for a search engine.

    There are several rules:

    • A few words in quotation marks should fall into one token
      This "huge test" is pointless => this,huge test,is,pointless
    • Words written through a hyphen also fall into one token.
      Words written through several hyphens (dashes), or with a hyphen at the beginning or at the end, fall into separate tokens.
      Suzie Smith-Hopper test--hyphens => Suzie,Smith-Hopper,test,hyphens.
    • Abbreviations (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.


    11. Letters in alphabetical order.


    Task:
    Choose 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. We correct gaps


    Task:
    Remove duplicate spaces and tabs, leave one space between words and two between sentences.

    Examples:
    Extra       spaces => Extra spaces
    Sentence.      Sentence. => Sentence.  Sentence.

    13. Repeating words under each other


    Task:
    Select duplicate words that are directly below each other.
    A monospace font is assumed. Lines longer than 32 characters are hyphenated.

    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 pretty obvious, so the task is to find the shortest solution.

    Examples:
    H- yes
    M- no

    15. Musical chords


    Task:
    Choose musical accords, such as Cmin , or Bmaj . Both a brief and complete entry are needed. For this problem, suppose that the chords E♯ , B♯ , F ♭ and C ♭ do not exist.

    For those interested, there is a good article on chords in Russian and an English Wikipedia article that uses the appropriate characters

    . Also note that a sharp (♯)
    is not the same as a 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 properly constructed regular expression. For starters, we restrict ourselves to literals (possibly escaped), classes, and several quantifiers.

    Examples:
    /regexp?/- yes
    regex- no

    18. IRC - Messages


    Task:
    Select a correctly formed IRC message.
    Here is a link to the Russian version of the specification.

    Examples:
    [_]!abc@test PRIVMSG #chat :Test- yes
    c.m!callum@lynx.io PRIVMSG #chat :Hello!- no

    Also popular now: