Level 1: Foundations · Lesson 1 of 30
What is Regex
A regular expression is a small pattern that describes text you want to find. Learn what a regex is, where it runs, and how a regex engine reads text from left to right.
A regular expression, usually shortened to regex, is a compact pattern that describes a piece of text. Instead of searching for one exact word, you can describe a shape: three digits, a word that starts with a capital letter, or a code like AB-123. A regex engine then scans your text and reports every place where that shape appears.
The simplest regex is just ordinary text. The pattern cat finds the three letters c, a and t next to each other, wherever they occur. It finds the word cat on its own, but it also finds the cat hidden inside concat or category. Later lessons show how to be more precise, but plain letters are always the starting point.
Most characters in a pattern match themselves. A few characters, called metacharacters, have special meanings: ., *, +, ?, [, ], (, ), {, }, ^, $, | and the backslash \. These symbols let you say things like any character, repeat this, or start of line. Learning regex is mostly learning what these few symbols do.
The engine works from left to right. It tries to match the pattern starting at the first character of the text. If that fails, it moves one position forward and tries again. When it finds a match, it records it and, in global mode, continues searching after the end of that match. That is why cat finds two results in cat and cat.
Regex is built into almost every programming language. JavaScript has regex literals such as /cat/g. Python uses the re module, PHP uses the preg_ functions, Java has java.util.regex, Go has the regexp package, C# has System.Text.RegularExpressions, Rust has the regex crate, and Ruby has regex literals like JavaScript. The core syntax you learn here works in all of them, with small differences we will point out along the way.
Regex is matched by default in a case-sensitive way. The pattern dog does not match Dog or DOG, because uppercase and lowercase letters are different characters. You can switch this off with a flag, which is covered in the flags lesson.
Use the tester on this page to experiment. Change the pattern, change the text, and watch the highlighted matches update. Trying small changes is the fastest way to build an intuition for how patterns behave.
Live tester
3 matches (JavaScript engine)
The cat sat on the mat next to another cat and a concatenated word.
Examples
Numbers
/42/gOrder 42 shipped. Order 142 is pending. Answer: 42.
Matches: “42”, “42”, “42”
A plain number pattern finds those digits anywhere, including inside the larger number 142.
Text
/hello/ghello world, Hello again, say hello
Matches: “hello”, “hello”
Matching is case-sensitive by default, so the capitalised Hello is skipped.
Codes
/AB-/gTickets AB-100 and AB-205 are open, CD-300 is closed.
Matches: “AB-”, “AB-”
Literal text works for code prefixes too; the hyphen is an ordinary character here.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match the word red in the list of fruit descriptions.
red apple, green pear, red cherry
Task 2: Match only the year 2024, not the other year.
Version 2024 released, build 2023 pending.
Task 3: Match the lowercase word dog but not the capitalised or uppercase versions.
dog, Dog, DOG and another dog