Skip to content
BENIBIX Learn

Regular Expression (Regex)

Development📄 Files & FormatsText

Also known as Regex, RegExp, Regular Expressions

A short pattern for text you want to find, like three digits and a dash.

Think of it like

Think of it like a search with blanks. Find three digits, a dash, then four digits, whatever they are.

Example

Emma needs every phone number in a long page. One regex finds all 40 of them in a second.

Why it matters

One line of regex can do the work of many lines of code. It finds or checks text.

Definition

A regex is a short pattern that describes text to look for. It can match a fixed word, or a shape like any three digits followed by a dash. Signs like \d stand for a digit, and {3} means three of them. Programs use regex to find, check or replace text, like checking that an email looks valid. Most languages and code editors support it.

Example code

python
import re
print(re.findall(r"\d{3}-\d{4}", "Call 555-0142 or 555-0199"))
# ['555-0142', '555-0199']
The pattern finds both numbers, no matter what digits they have.

Common mistake

Writing one huge regex nobody can read. Split it up or add comments, or the next person will not dare touch it.

Key takeaways

  1. 1
    A pattern, not a word. It describes a shape of text.
  2. 2
    Find, check, replace. Three common jobs.
  3. 3
    Keep it short. Long ones are hard to read.

Learn it properly