Python library/tools for combining and parsing using Regular Expressions in a maintainable way
This library also allows you to:
This library basically just gives you a way to combine Regular Expressions together and hook them up to some callback functions in Python.
(See the examples/ directory for a full code examples)
Say your fashionista friend must know what colors their friends like at certain times. Luckily for you two, your friend’s friends are blogging fanatics and you have downloaded thousands of text documents containing their every thought.
So you want to get (color and time) or [('green', datetime.time(23, 0))] out of text like:
blah blah blah go to the store to buy green at 11pm! blah blah
If you need scan/search/parse/transform some unstructured input and get some semi-structured data out of it RE|PARSE might be able to help.
Color: Basic Color: Expression: (Red|Orange|Yellow|Green|Blue|Violet|Brown|Black) Matches: Orange | Green Non-Matches: White Groups: - Color Time: Basic Time: Expression: ([0-9]|[0-2]) \s? (am|pm) Matches: 8am | 3 pm Non-Matches: 8a | 8:00 am | 13pm Groups: - Hour - AMPM
BasicColorTime: Order: 1 Pattern: | <Color> \s? at \s? <Time> # Angle brackets detonate expression groups # Multiple expressions in one group are combined together
from datetime import time def color_time(Color=None, Time=None): Color, Hour, Period = Color, int(Time), Time if Period == 'pm': Hour += 12 Time = time(hour=Hour) return Color, Time
from examples.colortime.functions import functions import reparse colortime_parser = reparse.parser( parser_type=reparse.basic_parser, expressions_yaml_path=path + "expressions.yaml", patterns_yaml_path=path + "patterns.yaml", functions=functions ) print(colortime_parser("~ ~ ~ go to the store ~ buy green at 11pm! ~ ~"))
pip install reparse
The code is located on Github. Send me suggestions, issues, and pull requests and I’ll gladly review them!
MIT Licensed! See LICENSE file for the full text.