Regular Expressions (RegEx): Complete Guide and Examples

Last update: May 2th 2026
  • What are regular expressions and their purpose: to define patterns to efficiently search, validate, and manipulate text.
  • Key elements: literal characters, character classes, metacharacters, and quantifiers that allow the construction of precise patterns.
  • Main applications: form validation, data search and extraction, text replacement, and log filtering.
  • Tools for testing and debugging: Regex101 and Regexr offer real-time testing and visualization.

RegEx examples

Regular expressions , or RegEx , are a fundamental tool in text handling and processing. They allow you to search for patterns , perform validations , and efficiently replace text fragments. Their use ranges from system administration to web development and programming in multiple languages.

In this article, we'll explore in detail how regular expressions work, what elements they comprise, and how they can facilitate advanced text manipulation. We'll also look at practical examples and their application in different scenarios.

What are regular expressions?

A regular expression is a sequence of characters that defines a search pattern within a text string. They are used in multiple programming languages ​​and data manipulation tools.

Originally, the concept of regular expressions emerged in the field of formal language theory and were popularized in the Unix world thanks to tools like grep, sed, and awk . Over time, they have become a key component in form validation , log manipulation, data processing, and much more.

Main uses of regular expressions

  • Data validation: They can be used to check if a email, an phone number or Postal Code is in the correct format.
  • Search and extraction of information: They facilitate the search for standards within long texts, such as urls o identification codes.
  • Text replacement: They allow you to modify text strings by replacing certain standards for other values.
  • Information leak: They are very useful for analyzing and structuring data in logs, databases or configurations.
  Complete Guide for Implementing Local LLMs in Business and Personal Environments

Basic RegEx Syntax

Basic elements of regular expressions

Regular Expressions (RegEx) are composed of different elements that allow you to define specific search patterns . The most commonly used ones are listed below:

1. Literal characters

These are the simplest elements in a regular expression. They exactly represent the typed character.

Example: Expression casa will search for the word “house” exactly as it appears in the text.

2. Character classes

They allow you to define a set of characters valid for a given position.

  • [ABC]: Matches a, b, or c.
  • [-0 9]: Matches any number from 0 to 9.
  • [^az]: Matches any character that is not a lowercase letter.

3. Metacharacters

These are special characters that have specific functions within regular expressions.

  • .: Represents any character except a line break.
  • \d: Matches any digit from 0 to 9.
  • \s: Represents any white space (including tabs and line breaks).

4. Quantifiers

They define the number of times a character or group of characters should appear.

  • +: One or more.
  • *: Zero or more.
  • ?: Zero or once.
  • {n,m}: Between nym repetitions.

Practical example of RegEx

Practical examples of regular expressions

1. Validating an email

We can use the following expression to check if an email has a valid format :

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

This expression validates emails from different domains by ensuring that they contain the "@" symbol and a valid domain.

2. Validation of a phone number

To verify the format of a phone number, we can use the following expression:

^\+?[0-9]{1,3}?[-.\s]?([0-9]{2,4}[-.\s]?){2,3}[0-9]{2,4}$

It allows for different formats, including optional country codes and separators such as hyphens or spaces.

3. URL Extraction

To extract URLs from text, we can use:

  Complete Guide to Spring Framework: Powering Java Development

https?://[\w.-]+(?:\.[a-zA-Z]{2,6})?

How to test and debug regular expressions

To ensure that a regular expression works correctly, we can use online tools such as:

These platforms allow you to enter a regular expression and test it with different text strings to visualize the results in real time.

RegEx Debugging

Regular expressions are an extremely useful tool for text manipulation. By understanding their syntax and applying them correctly, we can facilitate data validation , searching , and transformation in multiple fields, from programming to systems administration.