Online Tools Toolshu.com Log In Sign Up

20 Most Useful Regex Patterns Every Developer Should Know

Original Author:bhnw Released on 2026-04-05 10:44 14 views Star (0)

Introduction

Regular expressions are an unavoidable tool for developers, but few people memorize every syntax rule. The more practical approach: understand the core syntax, then look up ready-made patterns for specific scenarios.

This article compiles the 20 most frequently needed regex patterns in real development work, each with a pattern explanation and examples you can copy directly. A live testing tool link is included at the end.


Quick Syntax Reference

Before diving into specific patterns, here are the most essential metacharacters:

Symbol Meaning
. Matches any single character (except newline)
* Repeats the previous character 0 or more times
+ Repeats the previous character 1 or more times
? Repeats the previous character 0 or 1 time
^ Matches the start of a string
$ Matches the end of a string
\d Matches a digit, equivalent to [0-9]
\w Matches a letter, digit, or underscore
\s Matches a whitespace character
{n,m} Repeats n to m times
[abc] Matches any one character inside the brackets
(a|b) Matches either a or b

Part 1: User Information

1. Email Address

^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$

Explanation: Matches standard email format. The local part allows letters, digits, and ._%+-; the domain requires dot-separated segments; the TLD must be at least 2 characters.

Matches: user@example.com, hello.world+tag@sub.domain.org


2. US Phone Number

^(\+1[\s.-]?)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$

Explanation: Matches US phone numbers in common formats, with optional country code +1. Separators (space, dot, hyphen) between segments are optional.

Matches: (555) 123-4567, +1 555.123.4567, 5551234567


3. International Phone Number (E.164)

^\+[1-9]\d{7,14}$

Explanation: Matches E.164 international format — a + followed by country code and number, 8 to 15 digits total. Suitable for global systems.

Matches: +8613812345678, +14155552671


4. Username (Letters, Numbers, Underscore)

^[a-zA-Z0-9_]{4,16}$

Explanation: Common registration scenario — 4 to 16 characters, only letters, digits, and underscores allowed. Adjust the length range as needed.


5. Strong Password

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}$

Explanation: Minimum 8 characters, must contain at least one lowercase letter, one uppercase letter, one digit, and one special character. Uses lookahead assertions (?=...).


6. ZIP Code (US)

^\d{5}(-\d{4})?$

Explanation: Matches 5-digit US ZIP codes, with an optional 4-digit extension (ZIP+4 format).

Matches: 90210, 10001-1234


Part 2: Network Addresses

7. URL

^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)$

Explanation: Matches http and https URLs, including path, query parameters, and anchors.

Matches: https://toolshu.com/en/regex-visualizer, http://sub.example.com/path?q=1


8. IPv4 Address

^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$

Explanation: Precisely matches IPv4 addresses in the range 0.0.0.0 to 255.255.255.255.

Matches: 192.168.1.1, 255.255.255.0


9. IPv6 Address (Simplified)

^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$

Explanation: Matches fully expanded IPv6 addresses (without shorthand notation). Use a more comprehensive pattern for production environments.


10. Domain Name

^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$

Explanation: Matches standard domain names, supports multi-level subdomains, each segment up to 63 characters.


Part 3: Date and Time

11. Date (YYYY-MM-DD)

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Explanation: Matches ISO date format with month and day range constraints. Does not validate per-month day counts (e.g., February 30 would pass).

Matches: 2024-01-31, 2000-12-01


12. Time (HH:MM:SS)

^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$

Explanation: Matches 24-hour time, hours 00-23, minutes and seconds 00-59.


13. Date and Time Combined (ISO 8601)

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T([01]\d|2[0-3]):([0-5]\d):([0-5]\d)(Z|[+-]\d{2}:\d{2})?$

Explanation: Matches ISO 8601 datetime format with optional timezone offset.

Matches: 2024-03-15T14:30:00Z, 2024-03-15T14:30:00+08:00


Part 4: Content Formats

14. Integer (Including Negative)

^-?[1-9]\d*$|^0$

Explanation: Matches integers, allows negative sign, no leading zeros (01 does not match). 0 is handled separately.


15. Decimal Number (Including Negative)

^-?([1-9]\d*|0)\.\d+$

Explanation: Matches numbers with a decimal point; digits after the point are required. Does not match 1. (trailing dot with no digits).


16. Hexadecimal Color

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

Explanation: Matches CSS color values, supports both 3-digit and 6-digit shorthand.

Matches: #FF5733, #fff


17. HTML Tag (Paired)

<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>(.*?)<\/\1>

Explanation: Matches paired HTML tags and their content. Note: using regex to parse complex HTML is unreliable — use a DOM parser for complex scenarios.


18. Blank Line

^\s*$

Explanation: Matches lines containing only whitespace or completely empty lines. Commonly used when cleaning text to filter out blank lines.


19. Credit Card Number (Basic)

^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12})$

Explanation: Matches major card formats — Visa (starts with 4), Mastercard (51-55), Amex (34/37), Discover (6011/65). Format validation only; use Luhn algorithm for full verification.


20. Semantic Version Number (SemVer)

^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$

Explanation: Matches semantic version numbers per the SemVer specification, including optional pre-release and build metadata.

Matches: 1.0.0, 2.3.1-alpha.1, 1.0.0+build.123


Important Notes

1. Syntax Differences Across Languages

Regex support varies slightly by language. Older JavaScript versions do not support lookbehind; Python's re module does not support atomic groups by default. Always validate patterns in your target language environment after copying.

2. Escaping in String Literals

When writing regex inside a string, backslashes need to be doubled. For example, \d must be written as '\\d' in a Python string, or use a raw string: r'\d'.

3. Format Validation Is Not Data Validation

Regex only validates format. ID check digit verification, whether an email domain actually exists, whether a phone number is active — these all require additional logic.


Live Testing

To debug or verify any of these patterns, use the two tools available on toolshu.com:

  • Regex Visualizer: Parses your regex into a graphical automaton diagram, making the match structure intuitive
  • Regex Tester: Enter a pattern and test text for real-time highlighted match results

Both tools run entirely in your browser with no login required.

发现周边 发现周边
Comment area

Loading...