Regex Cheat Sheet

Posted in

Regex Cheat Sheet
vinaykhatri

Vinay Khatri
Last updated on October 9, 2024

    When it comes to extracting information or pattern from a string, we use regular expressions. In a regular expression, we use the ASCII code to perform the matching string operation.

    Many form validation operations are made by using regular expressions. When a user enters some information, at the same time, the form validates that data using a regular expression.

    Apart from validation, there are many other applications of regular expressions, such as parsing, replacing strings, passing translating data to different formats, and extracting information from the web. Many high-level programming languages also support regular expressions, and syntax of the same is similar for each programming language.

    What is Regex?

    regex stands for regular expression , and it is a technique to search string patterns from a string. It is used by many text editors, such as Sublime, Notepad++, Brackets, and Microsoft Word for search and replaces operations.

    Regex Cheat Sheet

    Anchor :

    Expression Description Example: Output Match
    ^ To check the starting point of a string. ^Tech Any string starting with “Tech.” Output: TechGeekbuzz
    $ To check the end of a string. $buzz TechGeekbuzz

    Characters:

    Expression Description Example: Output Match
    \d A digit from 0 to 9 “TechGeekbuzz \d\d\d\d” TechGeekbuzz2021
    \w Any ASCII letter, digit and underscore. “Tech\wGeekbu\w\w” Tech_Geekbuzz
    \s Whitespace. “Tech\sGeekbuzz” Tech Geekbuzz
    \D A character but not a digit. “Tech\Deekbuzz” TechGeekbuzz
    \W A character, which is not a word. “\WTechGeekbuzz” +TechGeekbuzz
    . Any character excluding the line break. T.B TGB; TAB; TBB; ..
    \ Escape next special character. TGB\\ TGB\

    Quantifiers:

    Expression Description Example: Output Match
    * Zero or more times. TGB* Matches a string that has TG followed by zero or more G Output: TG; TGB TGBBB;
    + One or more. TGB+ Matches a string that has TG followed by one or more G Output: TGB; TGBB;
    {\d } Exactly. TGB{5} Match a string TG followed by exactly 5 B’s; Output: TGBBBBB
    {\d,\d} In between. TGB{3,5} Match a string TG followed by 3 up to 5 B’s; Output: TGBBB; TGBBBB; TGBBBBB;
    ? Once or none. TGBs? Output: TGBs; TGB;

    Logic:

    Expression Description Example: Output Match
    | Or operator. 1|2 Either be 1 or 2
    () Group. T(echGeekBuzz|GB) TechGeekBuzz; TGB;
    \1 Group the content by 1. G(\w) \1 gle G oo gle; Gaagle; Gbbgle; ….
    \2 Group the content by 2. “(\d\d) (\w\w) = \2 \1” 12 aa = aa 12; 11  bb = bb 11; ….;

    Brackets:

    Expression Description Example: Output Match
    [  ] Any character from the bracket. “P[ae]n” Pan; Pen;
    - Sets a range. [a-z] a; b; c; …;
    [^] Any character except those that are in brackets. [^a-z] A; B; C; …; 1; 2; …
    [\d\D] Any character whether it is a digit or a non-digit.

    Conclusion

    This regex cheat sheet will help you through all your needs while dealing with regular expressions. Whether you're programming casually or involved in professional application development, the need to work with regular expressions is always there.

    People are also reading:

    Leave a Comment on this Post

    0 Comments