DEV Community

zohaib hassan
zohaib hassan

Posted on

Regex in 10 Minutes — The Only Guide Beginners Actually Need

Regex in 10 Minutes — The Only Guide Beginners Actually Need

Regex has a reputation for being unreadable.

This:

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Enter fullscreen mode Exit fullscreen mode

...looks like random keyboard smashing.

But it’s actually a valid email validator.

By the end of this guide, you’ll understand every character in it.


What Is Regex?

Regular expressions (regex) are patterns used to match combinations of characters in strings.

Almost every programming language supports them.


Common Use Cases

  • Validate emails, phone numbers, URLs
  • Search and replace text
  • Extract data from strings
  • Parse logs
  • Search within code editors

The Building Blocks of Regex

Literal Characters

The simplest regex matches exact text:

```text id="lit1"
hello → matches "hello" in "say hello world"




---

## The Dot (.)

Matches any single character (except newline):



```text id="dot1"
h.llo → matches "hello", "hallo", "hxllo"
Enter fullscreen mode Exit fullscreen mode

Character Classes []

Match any ONE character inside brackets:

```text id="class1"
[aeiou] → any vowel
[0-9] → any digit
[a-z] → lowercase letters
[A-Z] → uppercase letters




---

## Quantifiers

Control how many times a pattern repeats:

* `*` → 0 or more
* `+` → 1 or more
* `?` → 0 or 1 (optional)
* `{3}` → exactly 3 times
* `{2,5}` → between 2 and 5 times

---

## Anchors

* `^` → start of string
* `$` → end of string

Example:



```text id="anchor1"
^hello$ → matches only "hello" exactly
Enter fullscreen mode Exit fullscreen mode

Special Character Classes

Pattern Meaning
\d Any digit (0–9)
\w Word character (a–z, A–Z, 0–9, _)
\s Whitespace
\D Non-digit
\W Non-word character

Real-World Examples

Validate an Email

```regex id="email1"
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$




### Breakdown:

* `^` → start of string
* `[a-zA-Z0-9._%+-]+` → valid username characters
* `@` → literal symbol
* `[a-zA-Z0-9.-]+` → domain name
* `\.` → literal dot
* `[a-zA-Z]{2,}` → domain extension
* `$` → end of string

---

## Validate a URL



```regex id="url1"
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}
Enter fullscreen mode Exit fullscreen mode

Extract Numbers from Text

```regex id="num1"
\d+




Example:



```text
"Order #12345 costs $99"
Enter fullscreen mode Exit fullscreen mode

Matches:

  • 12345
  • 99

Validate Pakistani Phone Number

```regex id="pk1"
^(+92|0)[0-9]{10}$




---

## Remove Extra Whitespace



```regex id="space1"
\s+
Enter fullscreen mode Exit fullscreen mode

Replace with a single space.


Test Your Regex Instantly

Instead of guessing inside your code, test patterns live here:

👉 https://onlinefreetools.online/tools/regex-tester

You can:

  • Paste your regex
  • Test input strings
  • See matches highlighted in real time

Common Mistakes Beginners Make

1. Forgetting to Escape Characters

  • . means any character
  • \. means literal dot

2. Missing Anchors

Without ^ and $, your pattern may match partial strings.

Example:

Bad:

email regex without anchors
Enter fullscreen mode Exit fullscreen mode

It may match invalid inputs like:
notanemail@valid.com!!!


3. Greedy Matching

.* matches everything possible.

Use .*? for lazy matching when needed.


4. Not Testing Edge Cases

Always test:

  • Empty strings
  • Very long inputs
  • Special characters

Regex Cheat Sheet

Symbol Meaning
. Any character
\d Digit
\w Word character
\s Whitespace
^ Start of string
$ End of string
* 0 or more
+ 1 or more
? Optional
{n} Exactly n times
[abc] Any of a, b, c
[^abc] Not a, b, or c
(abc) Group
`a b` a or b

Final Thoughts

Regex looks scary at first, but it becomes intuitive very quickly once you understand the building blocks.

The key is simple:

  • Learn patterns, not memorization
  • Test everything visually
  • Start small, build gradually

Practice here:

👉 https://onlinefreetools.online/tools/regex-tester

Within a week, regex will feel natural.


Written by Zohaib Hassan

OnlineFreeTools.online — free browser tools for developers

Top comments (0)