Regex

Table of contents

Summarise with:

The regular expressions, commonly known as regex (short for «regular expressions»), are a sequence of characters that define an search pattern.

These expressions are used in a variety of programming languages and tools to perform operations on text strings, such as searching, matching, substitution and validation.

What is a regular expression?

A regular expression is a set of characters describing a pattern which may match one or more text strings. This pattern is designed to identify sequences of characters within a text that follow certain rules or structures.

In other words, regexes make it possible to search for specific patterns, such as email addresses, telephone numbers, words or even complex grammatical structures, in large blocks of text.

The power of regular expressions lies in their ability to describe not only simple matches, but also complex and sophisticated patterns.

For example, a regular expression can be used to check whether a text string follows the format of an e-mail address, whether it contains a telephone number or whether it has a specific sequence of characters.

What are regular expressions for?

The regular expressions have multiple applications in the field of computer science and software development. Some of the most common functions are:

  • Searching and pattern matching: They allow you to find matches within text strings accurately and efficiently. For example, search for all email addresses in a text file.
  • Data validation: Regexes are frequently used to validate input data, such as phone numbers, postal codes, URLs and email addresses. This is a very common method in web forms to ensure that users enter data in the correct format.
  • Text substitutionOne of the most useful features is the ability to perform text replacements based on a specific pattern. For example, replacing all occurrences of a word with another word within a document.
  • Text analysisRegular expressions are also used in text processing and data analysis tools to extract valuable information from large volumes of text.

Examples of regex

To further illustrate the use of regular expressions, Let us look at some examples:

Matching an e-mail address

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

This regular expression looks for an email address that follows the standard format: a username that can contain letters, numbers and some special characters, followed by a symbol @, and then a domain, followed by a . and a length of at least two letters.

Matching a phone number in international format

+?[0-9]{1,4}[-.s]?[0-9]{1,3}[-.s]?[0-9]{3,4}[-.s]?[0-9]{3,4}

This regular expression can match telephone numbers in different formats, optionally allowing the symbol + at the beginning, followed by groups of numbers separated by dashes, dots or spaces.

Matching a date in dd/mm/yyyy format:

b(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/d{4}b

This regex looks for dates in day/month/year format. It ensures that the day is between 01 and 31, the month is between 01 and 12, and the year contains exactly four digits.

Advanced features of regular expressions

Some additional functionalities of the regex are:

  • Grouping and catchesUsing parentheses, you can group parts of a regular expression and capture those groups for later use or analysis. For example, in (d{4})-(d{2})-(d{2}), parentheses allow you to capture the year, month and day of a date in yyyy-mm-dd format.
  • QuantifiersQuantifiers allow you to specify how many times a character or group of characters should appear in a match. For example, a+ means «one or more times», while a{3} means «exactly three times».
  • AlternationThe symbol | allows you to define alternatives within a regular expression. For example, colour|colour would match both spelling variants of the word.

We offer you training courses related to programming languages:

Share in:

Related articles

Explainability

In the context of artificial intelligence (AI) and machine learning, explainability refers to the ability to understand and explain in a clear and detailed way how a machine learning model generates predictions, makes decisions, or performs

Failover

What is failover? Failover is an automatic or manual process designed to ensure the continuity of a service or system in the event of a failure. This mechanism transfers the operations of a system

Output management system

An Output Management System (OMS) is a software solution designed to manage, control and optimise the production and distribution of documents and information generated by an organisation's business systems. These systems allow to centralise the flow of data coming from

Encoder

An encoder is a device that converts a linear or angular position into an electrical signal, used to determine the position, velocity or direction of an object. This device is essential in automation and control systems, as it provides precise and accurate information about the position, velocity or direction of an object.

Scroll to Top