0%

Cool RegEx Examples 2

1- Validating Email Addresses

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

This regular expression can be used to validate email addresses in a form. It checks for a string of characters before the @ symbol, followed by a domain name and a top-level domain extension.

2- Matching URLs:

Regex: (http|https)://[a-zA-Z0-9./?=_-]+

This regular expression can be used to match URLs in a text file. It matches both http and https URLs and includes characters such as slashes, question marks, equal signs, and underscores.

3- Extracting Phone Numbers:

Regex: \d{3}-\d{3}-\d{4}

This regular expression can be used to extract phone numbers from a text file or a database. It matches a pattern of three digits, followed by a hyphen, three more digits, another hyphen, and four more digits.

4- Matching HTML Tags:

Regex: <([a-z]+)([^<]+)(?:>(.)</\1>|\s+/>)

This regular expression can be used to match HTML tags in a string. It matches opening and closing tags and includes attributes such as classes and ids.

5- Extracting Zip Codes:

Regex: \b\d{5}(?:-\d{4})?\b

This regular expression can be used to extract zip codes from a text file or a database. It matches a pattern of five digits, optionally followed by a hyphen and four more digits.

6- Removing Punctuation:

Regex: [^\w\s]|_

This regular expression can be used to remove punctuation from a string. It matches any character that is not a word character or a whitespace character, as well as the underscore character.

7- Matching Credit Card Numbers:

Regex: \b(?:\d[ -]*?){13,16}\b

This regular expression can be used to match credit card numbers in a text file or a database. It matches a pattern of 13 to 16 digits, with optional spaces or hyphens between them.

8- Matching Dates:

Regex: \b\d{1,2}[/-]\d{1,2}[/-]\d{4}\b

This regular expression can be used to match dates in a text file or a database. It matches a pattern of one or two digits for the month and day, followed by a forward slash or hyphen, and four digits for the year.

9- Matching Hashtags:

Regex: #\w+

This regular expression can be used to match hashtags in a social media post. It matches the pound sign followed by one or more word characters.

10- Matching IP Addresses:

Regex: \b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\b

This regular expression can be used to match IP addresses in a text file or a database. It matches a pattern of four groups of one to three digits, separated by periods.

Welcome to my other publishing channels