Skip to main content

A Python port of Lua's LPeg pattern matching library

Project description

PPeg is a pattern matching library for Python, based on Parsing Expression Grammars (PEGs). It’s a port of the LPeg library from Lua.

Warning

PPeg is still experimental. The API and semantics are not stable.

Usage

Unlike the re module [1], PPeg patterns can handle balanced sequences

>>> from _ppeg import Pattern as P
>>> pattern = P.Grammar('(' + ( (P(1)-P.Set('()')) | P.Var(0) )**0 + ')')
>>> pattern('(foo(bar()baz))').pos
15
>>> pattern('(foo(bar(baz)').pos
-1
>>> capture = P.Cap(pattern)
>>> capture('(foo(bar()baz))').captures
['(foo(bar()baz))']

This example corresponds roughly to the following LPeg example

> lpeg = require "lpeg"
> pattern = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" }
> pattern:match("(foo(bar()baz))") -- Lua indexes begin at 1
16
> pattern:match("(foo(bar(baz)")
nil
> capture = lpeg.C(pattern)
> capture:match("(foo(bar()baz))")
"(foo(bar()baz))"
[1]Some regular expression implementations (e.g. PCRE, regex) support recursive patterns, which can match balanced sequences.

Modules

  • _cpeg.c
  • _ppeg.c
    • includes lpeg.c

Project details


Release history Release notifications

History Node

0.9.4

This version
History Node

0.9.3

History Node

0.9.2

History Node

0.9.1

History Node

0.9

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
PPeg-0.9.3.tar.gz (45.7 kB) Copy SHA256 hash SHA256 Source None Nov 5, 2015

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page