DEV Community

Jae Jeong
Jae Jeong

Posted on

Intro to Regular Expressions

Introduction

Regular expressions, or regex, are sequences of characters that define a search pattern that is primarily used for pattern matching within strings. They are commonly used in validating, searching, replacing, or extracting specific patterns from strings. Common uses are finding or matching strings that match a required format like phone numbers, email addresses, or dates.

Character Classes

Character Class Syntax Description
Character Set [ABC] Matches any character in the set
Negated Set [^ABC] Matches any character not in the set
Range [A-Z] Matches any characters in between the two specified characters, inclusively
Dot . Matches any characters except line breaks
Word \w Matches any word character (alphanumeric & underscore) Equivalent to [A-Za-z0-9_]
Not Word \W Matches any characters that are not a word character
Digit \d Matches any number (0-9)
Digit \D Matches any character that is not a number

Anchors

Anchor Syntax Description
Beginning ^ Matches the beginning of the string
End $ Matches the end of the string
Word Boundary \b Matches a word boundary position between a word character and non-word character
Not Word Boundary \B Matches any position that is not a word boundary

Reserved Characters

In regular expressions, some characters, (+ * ? ^ $ \ . [ ] { } ( ) | /) are reserved for specific purposes, as seen above. To represent these characters as a literal character, they should be preceded by a backslash ().

Quantifiers & Alternation

Syntax Description
Plus + Matches 1 or more of the preceding token
Star * Matches 0 or more of the preceding token
Quantifier {1,3} Matches the specified quantity of the preceding token. {1,3} will match 1 to 3 and {3} will match exactly 3. {3,} will match 3 or more.
Optional ? Matches 0 or 1 of the preceding token
Lazy ? Makes the preceding quantifier lazy, making it match as few characters as possible
Alternation | Matches the expression before or after the |

Examples

Example Syntax
Phone Number ^(\d{3})\s?\d{3}[-\s]?\d{4}$
Email Address ^\w+@[a-zA-Z_]+?.[a-zA-Z]{2,3}$
Date (MM/DD/YY) ^(0[1-9]|1[0-2])/(0[1-9]|[12]\d|3[01])/(\d{2})$

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay