stringparser Library
stringparser is a Python library that provides an easy-to-use interface for pattern matching and information extraction from strings, built on top of the 'parsy' library. It simplifies the process of defining structured patterns to parse text into Python dictionaries. The current version is 0.7, and releases are generally made on an as-needed basis rather than a fixed cadence.
Common errors
-
stringparser.ParsingError: Expected '...', got '...' at ...
cause The input string provided to `parser.parse()` did not match the pattern defined in `StringParser` or `Pattern` object.fixCarefully compare your input string with the pattern. Ensure all literal characters match and all capture groups (`{key:type}`) are correctly formatted and present in the input. -
TypeError: argument of type 'list' is not iterable (or similar TypeError with non-string/Pattern objects)
cause You attempted to initialize `StringParser` with an unsupported object type (e.g., a list, integer, or `None`) instead of a pattern string or a `Pattern` object.fixInitialize `StringParser` only with a valid string representing the pattern or an instance of `stringparser.Pattern`. -
AttributeError: 'StringParser' object has no attribute 'match' (or 'search')
cause Misconception that `stringparser` objects have `re`-like methods (`match`, `search`) for regex-style operations.fixUse the `parse()` method on your `StringParser` instance to attempt to parse the entire input string according to the defined pattern. `stringparser` is not a regex wrapper.
Warnings
- gotcha stringparser patterns are NOT regular expressions. While they define matching rules, they use a distinct syntax based on 'parsy' for structured parsing, not regex.
- gotcha As a 0.x version library, stringparser's API stability is not strictly guaranteed. Minor versions (e.g., 0.7 to 0.8) may introduce backward-incompatible changes.
- gotcha Input strings must match the defined pattern exactly. If a portion of the input does not conform to the pattern, a `ParsingError` will be raised, unlike regex which might partially match or return None.
Install
-
pip install stringparser
Imports
- StringParser
from stringparser import StringParser
- Pattern
from stringparser import Pattern
Quickstart
from stringparser import StringParser
# Define a parser using a pattern string
parser = StringParser("Hello {name:s}! Your age is {age:d}.")
# Parse a matching string
result = parser.parse("Hello Alice! Your age is 30.")
print(result)
# Expected output: {'name': 'Alice', 'age': 30}
# Example with a different pattern and input
date_parser = StringParser("{day:d}/{month:d}/{year:d}")
date_result = date_parser.parse("25/12/2023")
print(date_result)
# Expected output: {'day': 25, 'month': 12, 'year': 2023}