Common Regex Patterns: Email, Phone, URL, ZIP, Date Examples and Pitfalls
Regular expressions are a powerful tool for searching, extracting, and replacing text in one go. This article introduces practical regex patterns you will use often in real work, such as email, phone number, URL, postal code, date, and full-width/half-width conversion, along with the basics of metacharacters, quantifiers, and groups, plus the pitfalls to watch for.
You want to pull only specific lines out of a log, check whether an entered email address has the right format, or fix inconsistent wording across a document in one shot. Regular expressions (regex) handle these "work with text patterns" tasks all at once. This article introduces regex patterns you will use often in practice, with examples, while explaining the foundational metacharacters and quantifiers and the pitfalls you cannot ignore when using regex.
What Is a Regular Expression
A regular expression is a small notation for expressing a "pattern" in text. Conditions like "three digits in a row," "with an @ sign in between," or "at the start of a line" can be written as a short sequence of symbols. They work in almost every environment, from text editor search-and-replace to programming languages, databases, and command-line tools, so once learned they remain useful for a long time.
Regex use breaks down into three main jobs: judging whether a string matches a pattern (validation), extracting the needed part from text, and replacing the matched part with another string. The fastest way to confirm a pattern works as intended is to test it as you go. To check behavior on hand, enter your pattern and test string into the Regex Checker, and it will visualize what matches on the spot.
Metacharacters and Quantifiers to Know First
The source of regex power is metacharacters: symbols that carry special meaning rather than representing the literal character. Once you know the main ones, you can read and write most patterns.
| Symbol | Meaning |
|---|---|
. | Any single character (except newline) |
[ ] | Character class. [abc] is any one of a, b, c |
^ | Start of line (string) |
$ | End of line (string) |
| | Alternation (OR). cat|dog is cat or dog |
( ) | Grouping and capturing |
Used together with these are quantifiers, which specify how many times the preceding character or group repeats.
*… zero or more repetitions+… one or more repetitions?… zero or one (makes the preceding element optional){n}… exactly n times.{n,}is n or more,{n,m}is n to m times
For example, \d is shorthand for a single digit, so \d{3} means "exactly three digits" and \d+ means "one or more digits." Likewise, \w represents letters, digits, and underscore, and \s represents whitespace. Combining this shorthand with metacharacters gives you the basic building blocks of patterns. When you want to treat a metacharacter as a literal character, escape it by placing a backslash before it, like \..
Groups, Alternation, and Capturing
Parentheses ( ) are used for grouping, treating several characters as one unit. For example, (ab)+ means "ab repeated" and matches ab, abab, ababab, and so on. Because a quantifier only affects the single preceding element, groups are essential when you want to repeat multiple characters together.
Groups have another important role: capturing, which lets you retrieve the matched part later. Writing a date as (\d{4})-(\d{2})-(\d{2}), for instance, lets you retrieve the year, month, and day as separate groups. During replacement you can reference captured content with notation like $1 or \1, so reformatting such as "change the date separators to slashes" can be done in one go. For work that combines extraction and replacement, like batch-fixing inconsistent wording, the Text Replace tool is handy.
Alternation with the pipe | is effective when combined with groups. Writing (https?|ftp) expresses "http or https or ftp" together. Once you master groups, quantifiers, and alternation, you can cover the majority of practical patterns.
A Collection of Practical Patterns
From here, we introduce concrete patterns that come up often in practice. Note that each one needs adjustment depending on your use case and tolerance. Use them as a starting point, and always verify whether they fit your data with the Regex Checker.
Email address (simple)
^[\w.+-]+@[\w-]+\.[\w.-]+$
This checks the minimal form of "characters before and after the @, with a dot in the domain." However, the correct email spec is extremely complex, and a regex that validates it completely is impractical. For form input, it is realistic to keep to a simple check like this and do the final confirmation by sending a verification email.
Phone number
^0\d{1,4}-?\d{1,4}-?\d{3,4}$
This example starts with 0 and allows hyphens to be present or absent. Because area code lengths vary by region, the digit counts are given a range. To limit to mobile numbers, you can write something like ^0[789]0-?\d{4}-?\d{4}$.
URL
https?://[\w.-]+(/[\w./?%&=-]*)?
The ? in https? matches both "http" and "https," followed by a domain and an optional path. URLs also get complex when written strictly, so simplifying them for your use case is practical.
Postal code (Japan)
^\d{3}-?\d{4}$
This allows both "123-4567" and "1234567." To require the hyphen, remove the ?.
Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
This restricts the month to 01-12 and the day to 01-31. However, regex alone cannot reject impossible dates like February 30. For strict date checks, it is safer to leave the work to date handling on the program side.
Patterns for Width and Whitespace Cleanup
In Japanese text, mixing full-width and half-width characters and extra whitespace often cause inconsistencies. Regex shines in this kind of cleanup work too.
- Collapse consecutive whitespace into one: replace
\s+with a single space. - Trim leading and trailing whitespace: replace
^\s+|\s+$with an empty string (combined with multiline mode). - Detect full-width digits:
[0-9]picks out only full-width digits. For full-width letters, use[A-Za-z].
For example, when you want to "find numbers entered in full-width and convert them to half-width," first locate the targets with [0-9] and convert them with a replacement. This kind of paired search-and-replace can be done quickly using regex mode in the Text Replace tool. When you are unsure whether a pattern matches as intended, confirm the match range in the Regex Checker before replacing, which prevents accidents where unexpected parts get rewritten.
Why You Should Not Over-Trust Regex
Regex is not all-powerful. Because it is so convenient, leaning on it too much can lead you into traps. Finally, here are the points to watch in practice.
1. Not suited to complete spec validation. For things with complex specs like email addresses and URLs, trying to validate them perfectly with regex alone makes the pattern enormous and unmaintainable. It is realistic to keep to a simple check that "rejects clearly invalid input" and ensure true validity by other means (verification email, a dedicated library, date handling, and so on).
2. There are dialects by environment. Regex comes in several families (dialects), and the available shorthand and notation differ subtly by language and tool. A pattern that works in one tool will not necessarily work as-is in another, so when porting, checking behavior in the target environment is essential.
3. Greedy matching and performance. By default, quantifiers try to match as much as possible (greedy) and can unintentionally swallow a wide range. To get the shortest match, use *? or +?. Also, depending on how a pattern is written, it can trigger "catastrophic backtracking" that makes processing extremely slow, so always test complex patterns.
If you master the foundational metacharacters and quantifiers and grasp groups, regex becomes a powerful tool that dramatically widens your search, extraction, and replacement options. Use the patterns in this article as a starting point: first confirm behavior in the Regex Checker, then try your actual cleanup work in the Text Replace tool. The habit of verifying with your own hands is the shortcut to using regex safely.
Please note that this article is intended to provide general information. Because regex behavior can vary by the language and tool version you use, check the latest official documentation before relying on it for important processing.
FAQ
What is a regular expression?
A regular expression (regex) is a notation for expressing a "pattern" in text with short symbols. It can express conditions like "three digits in a row" or "with an @ sign in between," and is used to judge whether a string matches a pattern (validation), extract a needed part, or replace it with another string. It works across a wide range of environments, from text editors to programming languages.
What are metacharacters?
Metacharacters are symbols that carry special meaning rather than representing the literal character. Representative examples include "." for any single character, "[ ]" for a character class, "^" for the start of a line, "$" for the end, "|" for alternation (OR), and "( )" for grouping. Combine them with quantifiers (* + ? {n}) that express repetition counts, and you can read and write most patterns.
Can a regex fully validate an email address?
No, it is impractical in reality. The correct email spec is extremely complex, and a regex that validates it completely becomes enormous and unmaintainable. For forms, it is realistic to keep to a simple check like "characters before and after the @, with a dot in the domain" and ensure final validity by sending a verification email.
What is greedy matching?
By default, quantifiers such as * and + try to match as much as possible, which is called greedy matching. Because it can unintentionally swallow a wide range, when you want the shortest match, add "?" after the quantifier, as in "*?" or "+?", to make it a shortest match.
Are the patterns and text I enter in the Regex Checker sent to a server?
WAZA TOOLS' Regex Checker and Text Replace tools run entirely in your browser, so the patterns and text you enter are never sent to or stored on a server. They work on smartphones too, and you can confirm pattern behavior as many times as you like, for free.