Usage
The following gist is to help you getting started using this library
import pyperscan as ps
db = ps.BlockDatabase( # (1)!
ps.Pattern(b"foo.*bar", ps.Flag.SOM_LEFTMOST),
ps.Pattern(b"bar.*foo", ps.Flag.SOM_LEFTMOST, ps.Flag.DOTALL, tag="tag"), # (2)!
)
def on_match(ctx, tag, start, end): # (3)!
# ─┬─ ─┬─ ──┬── ─┬─
# │ │ │ └ match end index
# │ │ └ match start index (needs SOM_LEFTMOST flag)
# │ └ pattern tag, or index
# └ arbitrary object to store state
return ps.Scan.Continue # (4)!
ctx = ... # (5)!
scanner = db.build(ctx, on_match) # (6)!
scanner.scan(b"foobar") # (7)!
- Create a
BlockDatabase
holding the patterns to match against - Annotate patterns with flags and
tag
object to be able to distinguish, which pattern matched - Create a match handler function that will be run when a match is found
- You can stop or continue searching with the callback's return value
- Define a context object to store state in your match handler callback
- Build a scanner object to connect the database with your callback and context
- Feed data to execute the scanner