Regular-Expressions_regex
The ReadyRegex repository is a comprehensive collection of ready-to-use regular expressions (regex) for various common tasks. This repository aims to provide a valuable resource for developers, data analysts, and anyone working with text data, by offering a wide range of regex patterns specifically designed for tasks such as email validation, phone number parsing, credit card number recognition, and more.
Get latest regex at this github repository.
Star repository if you feel it helped.
To test regular expressions use regex101.
Whitespace
- Regex:
\s+
Phone Numbers
-
Validating phone numbers in different formats-
- Regex pattern:
^(?:\+\d{1,3}\s?)?(?:\(\d{1,4}\)\s?)?(?:\d{1,4}[\s-])?\d{1,10}$
- Example formats:
- +1 (123) 456-7890
- 5551234567
- (999) 9999-9999
- +1 5551234567
- +1 (416) 555 7890
- +33 123456789
- +91 9876543210
-
Normalizing phone numbers to a consistent format:
- Regex pattern:
^(\+?\d{1,3}\s?)?(\(?\d{1,4}\)?\s?)?(\d{1,})[-\s]?(\d{1,})$
- Example formats:
- +1 (123) 456-7890 (normalizes to +11234567890)
- 555 123 4567 (normalizes to 5551234567)
- +55 (11) 98765-4321 (normalizes to 5511987654321)
- +86 10 1234 5678 (normalizes to +861012345678
- +91 98765 43210 (normalizes to +919876543210)
Email Addresses
-
Validating email addresses:
- Regex pattern:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
- Example formats:
- john.doe@example.com
- jane_smith123@gmail.com
-
Extracting domain names from email addresses:
- Regex pattern:
@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
- Example formats:
- john.doe@example.com (extracts example.com)
- jane_smith123@gmail.com (extracts gmail.com)
URLs
-
Validating URLs:
- Regex pattern:
https?:\/\/[\w.-]+\.[\w.-]+[^\s]*
- Example formats:
-
Extracting different components of a URL(https://www.example.com/path/file.html?param1=value1¶m2=value2):
- Extracting protocol:
- Regex pattern:
^(https?):\/\/
(https?):\/\/([a-zA-Z0-9.-]+)
- Example format:
- Extracting path:
- Regex pattern:
(https?):\/\/[a-zA-Z0-9.-]+(\/[^?\s]*)?
(https?):\/\/[a-zA-Z0-9.-]+(\/[^?\s]*)?(\?[^#\s]*)?$
- Example format:
- ?param1=value1¶m2=value2
- Extracted Groups:
- https
- /path/file.html
- ?param1=value1¶m2=value2
Data Extraction
-
Extracting specific patterns from unstructured text:
- To extract dates:
(\d{1,2})\/(\d{1,2})\/(\d{4})
([01]\d|2[0-3]):([0-5]\d)
\d+\s[A-Za-z]+\s[A-Za-z]+,\s[A-Za-z]+\s\d+
- Example:
- Extract "123 Main St, New York 10001" from a text.
-
Extracting values from structured data formats:
- To extract values from XML:
<(.*?)>([^<]+)<\/\1>
- Example:
- Extract values between XML tags, such as
# data <person> <name>John Doe</name> <age>25</age> <email>johndoe@example.com</email> </person> # Expected Results name John Doe age 25 email johndoe@example.com
Credit Card
-
Validating credit card numbers:
- Regex Pattern:
^(?:\d{4}[ -]?){3}\d{4}$
-
Extracting credit card expiration dates:
- Regex Pattern:
(?:0[1-9]|1[0-2])\/20[2-9][0-9]
-
Matching CVV (Card Verification Value) codes:
- Regex Pattern:
^\d{3,4}$
- Example CVV Codes:
- 123
- 7890
- 4321
Connect with me -
Top comments (0)