# Regular Expressions (Regex)

> A regular expression is a pattern that matches text. It is used to search, validate and replace text in code, command line tools and infrastructure files.

- Source: https://www.itwonderlab.com/regex/
- Published: 2026-08-29
- Updated: 2026-08-29
- Author: Javier Ruiz Jiménez (https://www.javierruizjimenez.com/)
- Site: IT Wonder Lab (https://www.itwonderlab.com/)

---

A **regular expression** (regex) is a compact pattern that describes a set of strings. A tool reads the pattern and tells you whether a text matches, which part matched, or replaces the match. Regex appears everywhere in cloud and automation work: `grep` and `sed` in the shell, [Terraform](https://www.itwonderlab.com/terraform/) functions, [Ansible](https://www.itwonderlab.com/ansible/) filters, [Prometheus](https://www.itwonderlab.com/prometheus/) label matchers and input validation.

### Key concepts

- **Literal and metacharacters**: `abc` matches itself, while `.`, `*`, `+`, `?`, `^`, `$`, `[ ]`, `( )`, `{ }`, `|` and `\` have a special meaning.
- **Character class**: `[a-z0-9]` is one character from a set, `\d` is a digit, `\w` is a word character.
- **Quantifier**: how many times: `*` (0 or more), `+` (1 or more), `?` (0 or 1), `{3,5}`.
- **Anchor**: `^` start and `$` end of the text.
- **Group**: `( )` captures a part of the match for reuse.
- **Flavor**: not every tool speaks the same dialect (POSIX, PCRE, RE2), so check what yours supports.

### Learn it

Follow the [Regular expressions tutorial with examples](https://www.itwonderlab.com/regex-tutorial-examples/) and see [regex in Terraform, Ansible and Prometheus](https://www.itwonderlab.com/regex-terraform-ansible-prometheus/).
