Skip to content

Instantly share code, notes, and snippets.

@petergi
Created January 10, 2023 21:56
Show Gist options
  • Select an option

  • Save petergi/b6fcf00298a77ad4132b340f0a607a05 to your computer and use it in GitHub Desktop.

Select an option

Save petergi/b6fcf00298a77ad4132b340f0a607a05 to your computer and use it in GitHub Desktop.

Character classes

. Any character, except newline
\c Control Character
\w Word
\d Digit
\x Hexadecimal Digit
\O Octal Digit
\s Whitespace
\W Not word
\D Not digit
\S Not whitespace
[abc] Any of a, b, or c
[a-e] Characters between a and e
[1-9] Digit between 1 and 9

Anchors

^abc Start with abc
abc$ End with abc
^ Start of string, or start of line in multi-line pattern.
\A Start of string
$ End of string, or end of line in multi-line pattern
\Z End of string
\b Word Boundary
\B Not Word Boundary
\< Start of word
> End of word

Escaped and Special characters

\. \* \\ Escape special character used by regex
\t Tab
\n Newline
\r Carriage return
\v Vertical tab.
\f Form Feed.
\xxx Octal character xxx
\xhh Hex character hh.

Groups and Ranges

(abc) Capture group
`(a b)`
(?:...) Passive (non-capturing) group

Quantifiers

a* Match 0 or more
a+ Match 1 or more
a? Match 0 or 1
a{5} Match exactly 5
a{,3} Match up to 3
a{3,} Match 3 or more
a{1,3} Match between 1 and 3

Groups and Ranges

g global match
i Case insensitive match
m Multiple line match
s Treat as single line
x Allow comments and white space in pattern
e Eveluate replacement
U Ungreedy match

Assertions

?= Lookahead assertion
?! Negative lookahead assertion
?<= Lookbehind assertion
?!= or ?<! Negative Lookbehind assertion
?> Once-only Subexpression
?() Condition [if then]
`?() `
?# Comment

Examples

Matches jpg, gif or png image:

([^\s]+(?=.(jpg|gif|png)).\2)

Any number from 1 to 50 inclusive

(^[1-9]{1}|[1−4]1[0−9]1|^50$)

Valid hexadecimal colour code:

(#?([A-Fa-f0-9]){3}(([A-Fa-f0-9]){3})?)

Matches Letters, numbers and hyphens:

([A-Za-z0-9-]+)

Matches 8 to 15 character string with at least one upper case letter, one lower case letter, and one digit:

((?=.\d)(?=.[a-z])(?=.*[A-Z]).{8,15})

Matches US ZIP, ZIP + 4, and Canadian Postal Codes:

/(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)/

Email addresses

(\w+@[a-zA-Z_]+?.[a-zA-Z]{2,6})

HTML Tag

(\<(/?[^>]+)>)

Matches opening and closing HTML tags with content between:

/^<([a-z1-6]+)([^<]+)*(?:>(.*)<\/\1>| *\/>)$/

Matches http https ftp and file

/^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/

Matches Date (e.g. 1/4/2020):

(\d{1,2}\/\d{1,2}\/\d{4})

Will match dates with dashes, slashes or with spaces and optional time separated by a space or a dash:

dd - mm - yyyy, dd / mm / yyyy, dd mm yyyy, dd - mm - yyyy - hh: mm: ss or dd / mm / yyyy hh: mm: ss

/^(0?[1-9]|[12][0-9]|3[01])([ \/\-])(0?[1-9]|1[012])\2([0-9][0-9][0-9][0-9])(([ -])([0-1]?[0-9]|2[0-3]):[0-5]?[0-9]:[0-5]?[0-9])?$/

Validation for the formats dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY:

/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment