Skip to content

Instantly share code, notes, and snippets.

@vitorbritto
Last active September 27, 2025 02:41
Show Gist options
  • Save vitorbritto/9ff58ef998100b8f19a0 to your computer and use it in GitHub Desktop.
Save vitorbritto/9ff58ef998100b8f19a0 to your computer and use it in GitHub Desktop.

Revisions

  1. Vitor Britto revised this gist Jun 17, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion regex.md
    Original file line number Diff line number Diff line change
    @@ -63,7 +63,7 @@
    - `*`: 0 or more
    - `+`: 1 or more
    - `?`: 0 or 1
    - `{3`: Exactly 3
    - `{3}`: Exactly 3
    - `{3,}`: 3 or more
    - `{3,5}`: 3, 4 or 5

  2. vitorbritto revised this gist Jun 9, 2014. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions regex.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,9 @@

    # Regular Expressions


    ## Basic Syntax

    - `/.../`: Start and end regex delimiters |
    - `/.../`: Start and end regex delimiters
    - `|`: Alternation
    - `()`: Grouping

  3. vitorbritto created this gist Jun 9, 2014.
    131 changes: 131 additions & 0 deletions regex.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,131 @@

    # Regular Expressions


    ## Basic Syntax

    - `/.../`: Start and end regex delimiters |
    - `|`: Alternation
    - `()`: Grouping


    ## Position Matching

    - `^`: Start of string or start of line in multi-line mode
    - `\A`: Start of string
    - `$`: End of string or end of line in multi-line mode
    - `\Z`: End of string
    - `\b`: Word boundary
    - `\B`: Not word boundary
    - `\<`: Start of word
    - `\>`: End of word


    ## Character Classes

    - `\s`: Whitespace
    - `\S`: Not whitespace
    - `\w`: Word
    - `\W`: Not word
    - `\d`: Digit
    - `\D`: Not digit
    - `\x`: Hexade­cimal digit
    - `\O`: Octal digit


    ## Special Characters

    - `\n`: Newline
    - `\r`: Carriage return
    - `\t`: Tab
    - `\v`: Vertical tab
    - `\f`: Form feed
    - `\xxx`: Octal character xxx
    - `\xhh`: Hex character hh


    ## Groups and Ranges

    - `.`: Any character except newline (\n)
    - `(a|b)`: a or b
    - `(…)`: Group
    - `(?:…)`: Passive (non-c­apt­uring) group
    - `[abc]`: a, b or c
    - `[^abc]`: Not a, b or c
    - `[a-z]`: Letters from a to z
    - `[A-Z]`: Uppercase letters from A to Z
    - `[0-9]`: Digits from 0 to 9

    > Note: Ranges are inclusive.

    ## Quantifiers

    - `*`: 0 or more
    - `+`: 1 or more
    - `?`: 0 or 1
    - `{3`: Exactly 3
    - `{3,}`: 3 or more
    - `{3,5}`: 3, 4 or 5

    > Note: Quantifiers are greedy - they match as many times as possible. Add a ? after the quantifier to make it ungreedy.

    ## Escape Sequences

    - `\`:Escape following character. Used to escape any of the following metacharacters: {}[]()^$.|*+?\.
    - `\Q`: Begin literal sequence
    - `\E`: End literal sequence


    ## String Replacement

    - `$1`: 1st group
    - `$2`: 2nd group
    - `$n`: nth group
    - `$``: Before matched string
    - `$'`: After matched string
    - `$+`: Last matched string
    - `$&`: Entire matched string

    > Note: Some regex implem­ent­ations use \ instead of $.

    ## Assertions

    - `?=`: Lookahead assertion
    - `?!`: Negative lookahead
    - `?<=`: Lookbehind assertion
    - ``?!=, ?<!``: Negative lookbehind
    - `?>`: Once-only subexp­ression
    - `?()`: Condition if-then
    - `?()|`: Condition if-then-else
    - `?#`: Comment


    ## POSIX

    - `[:upper:]`: Uppercase letters
    - `[:lower:]`: Lowercase letters
    - `[:alpha:]`: All letters
    - `[:alnum:]`: Digits and letters
    - `[:digit:]`: Digits
    - `[:xdigit:]`: Hexade­cimal digits
    - `[:punct:]`: Punctu­ation
    - `[:blank:]`: Space and tab
    - `[:space:]`: Blank characters
    - `[:cntrl:]`: Control characters
    - `[:graph:]`: Printed characters
    - `[:print:]`: Printed characters and spaces
    - `[:word:]`: Digits, letters and underscore


    ## Pattern Modifiers

    - `g`: Global match
    - `i`: Case-i­nse­nsitive
    - `m`: Multi-line mode. Causes ^ and $ to also match the start/end of lines.
    - `s`: Single-line mode. Causes . to match all, including line breaks.
    - `x`: Allow comments and whitespace in pattern
    - `e`: Evaluate replac­ement
    - `U`: Ungreedy mode