stringparser Library

0.7 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `StringParser` instance with a pattern string and use it to extract data from input strings. The pattern uses `{key:type}` syntax for named capture groups and type conversion.

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}

view raw JSON →