Commonly Used Regex Patterns: Email Validation and More

Explore frequently used regex patterns for validating and searching different formats. Learn how to use regex for email validation and other practical use cases, enhancing your data handling skills.



Frequently Used Regex Patterns

Regex expressions are frequently used for searching or validating different formats. Below are some commonly used regex patterns.

Email Validation Pattern

Syntax

^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$
Explanation:

This pattern matches a valid email address. It ensures the presence of an "@" symbol, separates the username from the domain name with the "@", and checks for a valid domain name extension (e.g., ".com", ".org").

URL Validation Pattern

Syntax

^(https?:\/\/)?([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})(:[0-9]{1,5})?(\/.*)?$
Explanation:

This pattern matches a valid URL, including those starting with "http" or "https". It allows for an optional port number and path after the domain name.

Numeric Input Validation Pattern

Syntax

^\d+(?:\.\d+)?$
Explanation:

This pattern validates numeric input, including both decimal and integer values.

Date Validation Pattern (YYYY-MM-DD)

Syntax

^(?:19|20)\d\d-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])$
Explanation:

This pattern matches a valid date in YYYY-MM-DD format.

Time Validation Pattern (HH:MM AM/PM)

Syntax

^(1[0-2]|0?[1-9]):[0-5][0-9] (AM|PM)$
Explanation:

This pattern matches a time in 12-hour format with optional AM/PM designation.

Phone Number Validation Pattern

Syntax

^\+?[1-9]\d{1,14}$
Explanation:

This pattern validates a phone number, allowing an optional "+" sign at the beginning.

Zip Code Validation Pattern (US)

Syntax

^\d{5}(?:[-\s]\d{4})?$
Explanation:

This pattern matches a US ZIP code, with or without the optional ZIP+4 format.

Password Strength Validation Pattern

Syntax

^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[^\w\s]).{8,}$
Explanation:

This pattern validates a password with the following criteria: at least one uppercase letter, one lowercase letter, one digit, one special character, and a minimum length of 8 characters.

File Extension Validation Pattern

Syntax

^.+\.(jpg|jpeg|png|gif|pdf)$
Explanation:

This pattern matches common image and document file extensions (jpg, jpeg, png, gif, pdf).

IP Address Validation Pattern

Syntax

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

This pattern matches a valid IPv4 address in xxx.xxx.xxx.xxx format.

These are just a few examples of commonly used regex patterns. The specific pattern you'll need will depend on the particular data you're trying to validate.