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)!
  1. Create a BlockDatabase holding the patterns to match against
  2. Annotate patterns with flags and tag object to be able to distinguish, which pattern matched
  3. Create a match handler function that will be run when a match is found
  4. You can stop or continue searching with the callback's return value
  5. Define a context object to store state in your match handler callback
  6. Build a scanner object to connect the database with your callback and context
  7. Feed data to execute the scanner