Rebulk¶
Rebulk is a Python library that performs advanced searches in strings that would be
hard to implement using the re module
or string methods alone.
It provides building blocks such as Patterns, Match and Rule that let you build
a custom and complex string matcher through a readable, extendable, fluent API.
The project is hosted on GitHub: https://github.com/Toilal/rebulk.
Features¶
- Composable patterns — declare
string,regexandfunctionalpatterns in bulk on a singleRebulkobject using a fluent, chainable API. - Rich match model — every result is a
Matchobject withvalue,start,end,span,name,tagsand nestedchildren; theMatchescollection offers indexed lookups by name, tag, position and proximity. - Rule engine — express conditional post-processing (filtering, renaming, tagging,
appending) as
Ruleclasses with priorities and dependencies. - Chains and repeaters — compose ordered sequences of patterns with regex-like
quantifiers (
?,*,+,{n,m}). - Typed retrieval — bind a match name to its value type with
Keyfor type-safe access, and project matches onto a dataclass orTypedDictwithMatches.to(...). - Optional
regexbackend — enable theregexmodule (repeated captures) at runtime withREBULK_REGEX_ENABLED=1. - Fully typed — the package ships a
py.typedmarker and passesmypy --strict.
Installation¶
To enable the optional regex backend, install the
native extra and set the environment variable at runtime:
Rebulk requires Python 3.10 or later and has no runtime dependencies (the regex
backend is optional).
Quickstart¶
Regular expression, string and function based patterns are declared on a Rebulk
object. It uses a fluent API to chain string, regex and functional methods to
define the pattern types.
>>> from rebulk import Rebulk
>>> bulk = Rebulk().string('brown').regex(r'qu\w+').functional(lambda s: (20, 25))
Once the Rebulk object is fully configured, call matches with an input string to
retrieve all Match objects found by the registered patterns.
>>> bulk.matches("The quick brown fox jumps over the lazy dog")
[<brown:(10, 15)>, <quick:(4, 9)>, <jumps:(20, 25)>]
If several Match objects are found at the same position, only the longer one is kept
(this is the job of the default ConflictSolver rule).
>>> bulk = Rebulk().string('lakers').string('la')
>>> bulk.matches("the lakers are from la")
[<lakers:(4, 10)>, <la:(20, 22)>]
Where to go next¶
- Usage — build a
Rebulk, declare the three pattern types, configure pattern options, and read backMatch/Matches. - Rules & processors — write
Ruleclasses and use the built-in consequences and default processors. - API reference — the public symbols exported from the
rebulkpackage.