DEV Community

Kunal2468
Kunal2468

Posted on

Ready to use Register Expressions

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.

  1. Whitespaces
  2. Phone Number (various formats)
    1. Validating phone numbers in various formats
    2. Normalizing phone numbers to a consistent format:
  3. Email Addresses
    1. Validating email addresses:
    2. Extracting domain names from email addresses:
  4. URLs
    1. Validating URLs:
    2. Extract Protocol
    3. Extract Domain
    4. Extract Path
    5. Extract Query Parameters
  5. Data Extraction
    1. To extract dates:
    2. To extract time:
    3. To extract address:
    4. To extract values from XML:
  6. Credit Card
    1. Validating credit card numbers:
    2. Extracting credit card expiration dates:
    3. Matching CVV (Card Verification Value) codes:

Whitespace

  • Regex:
  \s+
Enter fullscreen mode Exit fullscreen mode

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

URLs

  • Extracting different components of a URL(https://www.example.com/path/file.html?param1=value1&param2=value2):

    • Extracting protocol:
    • Regex pattern:
      ^(https?):\/\/
    
    • Example format:
      • https://
      • http://
    • Extracting domain:
    • Regex pattern:
      (https?):\/\/([a-zA-Z0-9.-]+)
    
      (https?):\/\/[a-zA-Z0-9.-]+(\/[^?\s]*)?
    
    • Example format:
      • /path/file.html
    • Extracting query parameters:
    • Regex pattern:
      (https?):\/\/[a-zA-Z0-9.-]+(\/[^?\s]*)?(\?[^#\s]*)?$
    
    • Example format:
      • ?param1=value1&param2=value2
    • Extracted Groups:
      • https
      • /path/file.html
      • ?param1=value1&param2=value2

Data Extraction

  • Extracting specific patterns from unstructured text:

    • To extract dates:
    (\d{1,2})\/(\d{1,2})\/(\d{4})
    
    • Example:
      • Extract "12/31/2023" from a text.
    • To extract times:
    ([01]\d|2[0-3]):([0-5]\d)
    
    • Example:
      • Extract "18:45" from a text.
    • To extract addresses:
    \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}$
    
    • Example Credit Card Numbers:
      • 4111 1111 1111 1111
      • 5555-5555-5555-4444
      • 3782822463100058
  • Extracting credit card expiration dates:

    • Regex Pattern:
    (?:0[1-9]|1[0-2])\/20[2-9][0-9]
    
    • Example Expiration Dates:
      • 12/2023
      • 05/2025
      • 09/2030
  • Matching CVV (Card Verification Value) codes:

    • Regex Pattern:
    ^\d{3,4}$
    
    • Example CVV Codes:
      • 123
      • 7890
      • 4321

Connect with me -

Top comments (0)