Regular expressions pcre2 cheatsheet

Regular expressions pcre2: Common patterns

12 common Regular expressions pcre2 tasks.

Email address (simple)

A permissive check for [email protected]. Fully validating email is impractical, so confirm addresses by sending a message.

^[^\s@]+@[^\s@]+\.[^\s@]+$

Web URL

Match an http or https URL up to the next whitespace.

https?://[^\s/$.?#][^\s]*

IPv4 address (shape only)

Match four groups of 1 to 3 digits. It doesn't check that each is 255 or less.

\b(?:\d{1,3}\.){3}\d{1,3}\b

ISO date

Match dates like 2026-10-30. It doesn't check that the month or day are valid.

\d{4}-\d{2}-\d{2}

Hex colour

Match both the short #fff and the long #ffffff forms.

#(?:[0-9a-fA-F]{3}){1,2}\b

UUID

Match a UUID in the standard 8-4-4-4-12 format.

[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}

Semantic version

Match versions like 1.4.2, 2.0.0-beta.1 or 1.0.0+build.5.

^\d+\.\d+\.\d+(?:-[\w.]+)?(?:\+[\w.]+)?$

URL slug

Match lowercase words joined by single hyphens.

^[a-z0-9]+(?:-[a-z0-9]+)*$

Hashtag

Match a hash sign followed by word characters.

#\w+

Double-quoted string with escapes

Match a quoted string that may contain escaped quotes.

"(?:[^"\\]|\\.)*"

Trim whitespace

Match leading and trailing whitespace. Replace matches with nothing to trim.

^\s+|\s+$

Collapse repeated spaces

Match two or more whitespace characters, to replace with one space.

\s{2,}

Was this page helpful?