Regular expressions for beginners: the symbols you'll use most
Pulling every phone number out of a long document, or checking whether an email address is formatted correctly — that's what regular expressions (regex) are for. They look like a secret code at first, but a handful of symbols covers most everyday uses.
The most common symbols
.: any single character\d: one digit (0–9)\w: one letter, digit or underscore\s: one whitespace character (space, tab, line break)[abc]: any one of the characters in brackets; ranges like[0-9]work too^marks the start of a line,$the end
How many times?
*: zero or more+: one or more?: zero or one{3}: exactly 3,{2,4}: 2 to 4
Reading some examples
\(\d{3}\) \d{3}-\d{4}→ "(555) 123-4567", a common U.S. phone number format^\d{5}$→ exactly five digits from start to end, like a ZIP code[A-Z][a-z]+→ a capital letter followed by lowercase letters, like a capitalized word
Tips
- To match a special character literally (
.*?(and so on), put a\in front of it. A literal period is\. - Complex formats like email addresses are hard to validate perfectly with one pattern. Make it too strict and you'll reject valid input.
- Programming languages support slightly different regex features, so test in the tool you'll actually use.
Experiment with the regex tester.
What have you used regular expressions for? Share below.
Also shown in Regex Tester
This article is a post in the “News” room — keep the conversation going below. If you spot an error, use the correction and reply request form.
🔗 Copy link Copied
Live comments 0
No comments yet. Be the first to share a thought.