CommonRegex

repository·master·Indexed 23 days ago

https://github.com/madisonmay/commonregex

A Python library for extracting common data patterns such as emails, phone numbers, dates, and IPs from strings using pre-configured regular expressions. It provides a CommonRegex class for multi-pattern extraction and individual regex modules for specific data types.

Tokens
887
Snippets
4
Records
5
Agent score
32%

What's inside commonregex

  1. Reuse a CommonRegex instance for multiple segments

    master

    Instead of passing text to the constructor every time, you can create a single CommonRegex instance and call specific extraction methods on different strings of text.

    >>> parser = CommonRegex()
    >>> parser.times("When are you free?  Do you want to meet up for coffee at 4:00?")
    ['4:00']
  2. Use individual regex patterns directly

    master

    CommonRegex exposes its underlying regular expressions as individual modules (e.g., email, time). You can use these directly with Python's re module for tasks like substitution or finding iterators.

    >>> from commonregex import email
    >>> import re
    >>> text = "...get in touch with my associate at harold.smith@gmail.com"
    >>> re.sub(email, "anon@example.com", text)
    '...get in touch with my associate at anon@example.com'
    >>> from commonregex import time
    >>> for m in time.finditer("Does 6:00 or 7:00 work better?"):
    >>>     print m.start(), m.group()     
    5 6:00 
    13 7:00 
  3. Parse text using the CommonRegex class

    master

    To extract multiple types of information from a single string, instantiate the CommonRegex class with your text. You can then access specific data types via attributes like .times, .dates, .links, .phones, .phones_with_exts, and .emails.

    >>> from commonregex import CommonRegex
    >>> parsed_text = CommonRegex("""John, please get that article on www.linkedin.com to me by 5:00PM 
                                   on Jan 9th 2012. 4:00 would be ideal, actually. If you have any 
                                   questions, You can reach me at (519)-236-2723x341 or get in touch with
                                   my associate at harold.smith@gmail.com""")
    >>> parsed_text.times
    ['5:00PM', '4:00']
    >>> parsed_text.dates
    ['Jan 9th 2012']
    >>> parsed_text.links
    ['www.linkedin.com']
    >>> parsed_text.phones
    ['(519)-236-2727']
    >>> parsed_text.phones_with_exts
    ['(519)-236-2723x341']
    >>> parsed_text.emails
    ['harold.smith@gmail.com']