Logo

dev-resources.site

for different kinds of informations.

Intro to Regular Expressions

Published at
9/12/2024
Categories
regex
beginners
Author
jae_jeong_56b53bffb105974
Categories
2 categories in total
regex
open
beginners
open
Author
25 person written this
jae_jeong_56b53bffb105974
open
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})$
regex Article's
30 articles in total
Favicon
Here are 7 Regex tools that can save your life from hell πŸ”₯
Favicon
What are the benefits of using bounded quantifiers in regex
Favicon
Understanding Regex in Python: A Practical Example
Favicon
Coding challenge: Design and Implement an Advanced Text Search System
Favicon
Automating Email Validation with Python: A Step-by-Step Tutorial
Favicon
Streaming regex scanner β€” regexpscanner
Favicon
Unraveling the Magic of Regular Expressions: The Ultimate Guide to Mastering Sed, Gawk, and POSIX PatternsπŸš€
Favicon
Masking confidential data in prompts using Regex and spaCy
Favicon
Regular Expressions for Highlighting Comments in PyCharm
Favicon
Regex lookahead
Favicon
Easy to follow Regular Expression Cheatsheet
Favicon
πŸ“ Cross-Post Project Update: Regex, Bug Fixes, and More Regex!
Favicon
How to work with regular expressions
Favicon
Advent of Code 2024 - Day 3: Mull it Over
Favicon
Vim Regex Tricks - Capitalize Every First Letter
Favicon
Finally figured out a whole bunch of Nginx regex. It's more confusing than normal regex somehow
Favicon
From Regex Rampage to Lazy Bliss: My rjq Performance Adventure
Favicon
Regular Expressions
Favicon
Building a Regex Engine in Go: Introducing MatchGo
Favicon
Build up your confidence with Regex: 5 Techniques to make it STICK
Favicon
Mastering Regular Expressions: A Semantic Approach to Regex
Favicon
Regex for a Java Software Engineer
Favicon
Intro to Regular Expressions
Favicon
Intro to Regular Expressions
Favicon
The importance of the environment in Regex pattern matching
Favicon
js / ts - expressΓ£o regular
Favicon
A Guide to Splitting Strings in JavaScript by Regex
Favicon
Taming the Regex Beast: A Beginner's Guide to Regular Expressions
Favicon
The JS string replace() method
Favicon
Learn Enough Regex Without Losing Your Mind

Featured ones: