← Back to Blog

20 Common Regex Patterns Every Developer Should Know

Essential Regex Patterns

Here are 20 regex patterns you'll use regularly in your development work.

Validation Patterns

1. Email Address

``

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

`

2. URL

`

https?:\/\/[\w\-._~:/?#[\]@!$&'()*+,;=%]+

`

3. Phone Number (US)

`

\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}

`

Matches: (555) 123-4567, 555-123-4567, 555.123.4567

4. IPv4 Address

`

\b(?:\d{1,3}\.){3}\d{1,3}\b

`

5. Date (YYYY-MM-DD)

`

\d{4}-\d{2}-\d{2}

`

6. Time (HH:MM)

`

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

`

7. Hex Color

`

#[0-9A-Fa-f]{6}\b

`

8. Credit Card (Basic)

`

\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}

`

9. ZIP Code (US)

`

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

`

10. Username

`

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

`

Extraction Patterns

11. Extract Domain from URL

`

https?:\/\/([^/]+)

`

Group 1 captures the domain

12. Extract Hashtags

`

#(\w+)

`

13. Extract Mentions

`

@(\w+)

`

14. Extract Numbers

`

-?\d+\.?\d*

`

15. Extract HTML Tags

`

<([a-z]+)[^>]*>

`

Text Processing

16. Remove Extra Whitespace

`

\s+

`

Replace with single space

17. Match Words Only

`

\b\w+\b

`

18. Match Quoted Strings

`

"([^"]*)"

`

19. Match Parenthetical Content

`

\(([^)]+)\)

`

20. Split by Multiple Delimiters

`

[,;\s]+

``

Usage Tips

  • Always test patterns with edge cases
  • Consider using non-capturing groups (?:) when you don't need the capture
  • Anchor patterns (^ $) when validating entire strings
  • Use RegexSpark to test and debug your patterns in real-time
  • Try RegexSpark

    Test and debug your regular expressions in real-time with our free online tool.

    Test Regex Now