Skip to main content

Python implementation of Lua-style pattern matching

Project description

A Python 3.3+ implementation of the Lua language’s pattern matching functions. Lua’s pattern matching is simpler than regular expressions and lacks several features that regexes have, such as | for alternation, but also contains some features difficult or impossible to duplicate in most regex flavors, such as the ability to easily match a balanced pair of parentheses (or any two other characters).

Installation

pip install luapatt

Documentation

For documentation on how pattern matching works, please read the Lua reference manual. This library contains the following differences from stock Lua:

  • %c, %g, %p, and their negated counterparts are not available; attempting to use them will raise the built-in NotImplementedError.

  • Other character classes that rely on the meaning of a character call Python’s str.is* family of methods, and so use the Unicode definition of that meaning.

  • String positions are zero-based instead of one-based, reflecting the fact that Python is generally zero-based (as opposed to Lua, which has one-based indexes). This affects position captures and the indexes returned as the first two results from find().

  • Function return values are combined into a tuple, as is standard with Python. However, singleton tuples are not returned; the single value is returned directly instead.

  • gsub() does not return the number of substitutions by default, instead returning only the new string. To get the count, pass the named argument count=True to the call (which will result in a 2-tuple of the new string and the count).

  • An extra function, set_escape_char(), is provided to change the escape character. It takes one argument: the new escape character, which must be a str object of length 1. The escape character cannot be set to any of the other special characters. While it is possible to set it to a letter or number, this is not recommended as it may interfere with other aspects of pattern matching, and doing so may be disallowed in the future.

    • NOTE: Because set_escape_char modifies global state, it is not thread-safe.

  • Unlike Lua, which has no notion of a Unicode string and assumes all characters are one byte in length, this library operates on full Unicode strings (i.e. str objects). If you pass bytes objects to this library, the behavior is undefined.

Licensing

As with Lua itself, this library is released under the MIT License.

Project details


Download files

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

Source Distribution

luapatt-0.9.0b5.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

luapatt-0.9.0b5-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file luapatt-0.9.0b5.tar.gz.

File metadata

  • Download URL: luapatt-0.9.0b5.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for luapatt-0.9.0b5.tar.gz
Algorithm Hash digest
SHA256 0ec90c0ed6fa64df060b89ae4acc3bfe3153b9f4bd0b09f8d1911966a0cfb094
MD5 b800b0a8e4f694fb3c8f0dd63136cffb
BLAKE2b-256 236abf9ee08496d6697778b9269c04630853ca99459c5a9cc5624cf70ef9de7b

See more details on using hashes here.

File details

Details for the file luapatt-0.9.0b5-py3-none-any.whl.

File metadata

File hashes

Hashes for luapatt-0.9.0b5-py3-none-any.whl
Algorithm Hash digest
SHA256 b2e5470b2584594a8d858f70e15928cdc64251e9390a780a9fe163267b30d476
MD5 97906ccbad713dcceb5998d8b76e4806
BLAKE2b-256 093030a25919b0d34b4c9120c25d3521f11a7c3d7abd300dc103d9691cef103a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page