Regular expressions for all occasions

Discussion in 'Useful articles' started by LightWood, Dec 3, 2017.

  1. LightWood

    LightWood Moderator

    Joined:
    Nov 4, 2010
    Messages:
    2,082
    Likes Received:
    711
    We collect the people's list of regulars (regex) for all occasions, I'll start first. Who has their own - throw, add to the first post indicating the author. Regulars can sometimes work incorrectly, if that write off in this thread - we will find a solution.

    List of regular expressions and typical usage contexts
    Search for a number greater than 0
    Code (text):
    1. (?<=(\D|^))[1-9]\d*
    Use case:
    Снимок экрана от 2017-12-03 17-09-18.png

    In this example, the visible elements are searched for only, the invisible elements (having a width of 0px) are ignored, but sometimes there are misfires - when a site overlays elements (it is found in Google for example).

    Thanks: CSS, ZennoScript
    Parsing the text for sentences, seems author is ZennoScript, I have slightly modified
    Code (text):
    1. [А-ЯA-Z].{15,}?(\.|\!|\?)(?=\ |\r|\n|$)
    work example, initial data:
    result:
    Снимок экрана от 2017-12-03 17-12-23.png

    Thanks: ZennoScript
    Search for a word starting with a specific text (Thank ZennoScript for the modification)
    Code (text):
    1. (?i)\bgo.*?(?=\W|\ |\r|\n|$)
    Find in the text all the words beginning with go, for example: goal, god, google

    Thanks: CSS, ZennoScript
    Searches for a number in the range 400-699:
    Code (text):
    1. ^([4-6][0-9][0-9])$
    more about the range you can see here http://www.regular-expressions.info/numericranges.html

    Shared: CSS
    Virtual columns with separator ";"
    Code (text):
    1. (?<=(^|;)).*?(?=(;|$|\r\n))
    initial data: name;pass;mail
    result: the "column" you need by the match number:
    View attachment 8857
    use case: we work with the list, we disassemble by the pieces of the column as in the table

    Shares: CSS
    Get the file name from the windows path:
    Code (text):
    1. ([^\\]+$)
    initial data:
    result: ZennoPoster.exe

    Shared: CSS
    Search for strings NOT containing the specified (sub) string:
    Code (text):
    1. (?<=(^|\r\n))((?!(test)).)*(?=($|\r\n))
    initial data:
    result:
    Shared: CSS
    Take all the lines of more than 18 characters
    Code (text):
    1. (?<=(\r\n|^)).{18,}(?=(\r\n|$))
    Thanks: alekwuy
    Get all lines less than 18 characters
    Code (text):
    1. (?<=(\r\n|^)).{1,18}(?=(\r\n|$))
    Thanks: alekwuy
    Search all HTML tags
    Code (text):
    1. <.*?>
    Use case - clear text from HTML code

    Thanks: Trader1985
    Find all empty lines in the text
    Code (text):
    1. \n\r
    Use case - text correction

    Thanks: Trader1985
    Search from three or more blank lines
    Code (text):
    1. (?<=(\r\n){2})(\r\n){1,}
    Use case - text correction if we are satisfied with 2 blank lines

    Thanks: Trader1985
    Email:
    Code (text):
    1. [\.\-_A-Za-z0-9][email protected][\.\-A-Za-z0-9]+?[\.A-Za-z0-9]{2,}
    IP
    Code (text):
    1. (\d{1,3}\.){3}\d{1,3}
    URL
    Code (text):
    1. (http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?
    IP:PORT (for example for parsing a proxy)
    Code (text):
    1. (\d{1,3}\.){3}\d{1,3}:\d*
    Thanks: up_lvl


    Origin: Link
    Author: CSS
    Translate by: LightWood
     
    aleksa77 and Dimionix like this.
  2. ahweb

    ahweb Новичок

    Joined:
    Dec 3, 2013
    Messages:
    1
    Likes Received:
    1
    Very helpful. Thanks.
     
    LightWood likes this.
  3. LightWood

    LightWood Moderator

    Joined:
    Nov 4, 2010
    Messages:
    2,082
    Likes Received:
    711
  4. NICOLAS Christophe

    NICOLAS Christophe Client

    Joined:
    Jul 10, 2017
    Messages:
    2
    Likes Received:
    0
    Really helpful for a newbie like me :-)
     
  5. Smasher

    Smasher Новичок

    Joined:
    Jun 12, 2016
    Messages:
    7
    Likes Received:
    0
    me too brada <3
     

Пользователи просматривающие тему (Пользователей: 0, Гостей: 0)