Regular Expressions for Beginners: Patterns You Can Use Today
A gentle introduction to regex: the building blocks, a few useful patterns and how to test them safely.
What a regular expression is
A regular expression, or regex, is a pattern that describes text. Programs use it to find, validate or replace parts of a string, such as checking whether a field looks like a phone number.
The basic building blocks
A normal character matches itself. A dot matches any single character. \d matches a digit, \w a letter, digit or underscore, and \s a space. Quantifiers say how many: * means zero or more, + means one or more, ? means optional, and {3} means exactly three. Square brackets such as [abc] match one of the listed characters.
Anchors and groups
^ matches the start of the text and $ the end, so ^\d{6}$ matches exactly six digits, such as a PIN code. Parentheses make a group, which you can capture and reuse in a replacement.
A few useful patterns
Digits only: ^\d+$. A simple date such as 12-10-2026: ^\d{2}-\d{2}-\d{4}$. A rough email check: ^[^\s@]+@[^\s@]+\.[^\s@]+$. These are simple, so real-world validation may need more care.
Test before you trust
Use a tester with sample text that should match and text that should not. Be careful with nested repetition such as (a+)+, which can run extremely slowly on some inputs.
This guide is general information. Rules differ between institutions, so confirm requirements with your college or the portal you are using.