Skip to main content

Readable regular expressions for Python 3.6 and up.

Project description

cursive_re

Readable regular expressions for Python 3.6 and up.

Installation

pip install cursive_re

Examples

>>> from cursive_re import *

>>> hash = text("#")
>>> hexdigit = any_of(in_range("0", "9") + in_range("a-f") + in_range("A-F"))
>>> hexcolor = (
...     beginning_of_line() + hash +
...     group(repeated(hexdigit, exactly=6) | repeated(hexdigit, exactly=3)) +
...     end_of_line()
... )
>>> str(hexcolor)
'^\\#([a-f0-9]{6}|[a-f0-9]{3})$'

>>> hexcolor_re = compile(hexcolor)
re.compile('^\\#([a-f0-9]{6}|[a-f0-9]{3})$')

>>> hexcolor_re.match("#fff")
<re.Match object; span=(0, 4), match='#fff'>

>>> hexcolor_re.match("#ffff") is None
True

>>> hexcolor_re.match("#ffffff")
<re.Match object; span=(0, 7), match='#ffffff'>

>>> domain_name = one_or_more(any_of(in_range("a", "z") + in_range("0", "9") + text("-")))
>>> domain = domain_name + zero_or_more(text(".") + domain_name)
>>> path_segment = zero_or_more(none_of("/"))
>>> path = zero_or_more(text("/") + path_segment)
>>> url = (
...     group(one_or_more(any_of(in_range("a", "z"))), name="scheme") + text("://") +
...     group(domain, name="domain") +
...     group(path, name="path")
... )
>>> str(url)
"(?P<scheme>[a-z]+)://(?P<domain>[a-z0-9\-]+(?:\.[a-z0-9\-]+)*)(?P<path>(?:/[^/]*)*)"

Reference

cursive_re.compile

Compile a cursive_re expression to a real regular expression.

cursive_re.beginning_of_line

Matches the beginning of a line.

Examples:

>>> str(beginning_of_line())
... "^"

cursive_re.end_of_line

Matches the end of a line.

Examples:

>>> str(end_of_line())
... "$"

cursive_re.anything

Matches any character.

Examples:

>>> str(anything())
... "."

cursive_re.literal

Inserts a literal regular expression.

Examples:

>>> str(literal("\A\w"))
... "\A\w"

cursive_re.text

Matches the given string exactly, escaping any special characters.

Examples:

>>> str(text("abc"))
... "abc"

cursive_re.any_of

Matches any of the given characters.

Examples:

>>> str(any_of("ab"))
... "[ab]"

>>> str(any_of(text("ab")))
... "[ab]"

>>> str(any_of(text("[]")))
... "[\[\]]"

cursive_re.none_of

Matches none of the given characters.

Examples:

>>> str(none_of(text("ab")))
... "[^ab]"

>>> str(none_of(text("[]")))
... "[^\[\]]"

cursive_re.in_range

Matches a character in the given range.

Examples:

>>> str(in_range("a", "z"))
... "a-z"

cursive_re.zero_or_more

Matches zero or more of the given expr.

Examples:

>>> str(zero_or_more(text("a")))
... "a*"

>>> str(zero_or_more(group(text("abc"))))
... "(abc)*"

cursive_re.one_or_more

Matches one or more of the given expr.

Examples:

>>> str(one_or_more(text("a")))
... "a+"

>>> str(one_or_more(group(text("abc"))))
... "(abc)+"

cursive_re.maybe

Matches an expr if present.

Examples:

>>> str(maybe(text("abc")))
... "(?:abc)?"

cursive_re.repeated

Matches an expr repeated an exact number of times.

Examples:

>>> str(repeated(text("a"), exactly=5))
... "(?:a){5}"

>>> str(repeated(text("a"), at_least=1))
... "(?:a){1,}"

>>> str(repeated(text("a"), at_most=5))
... "(?:a){0,5}"

>>> str(repeated(text("a"), at_least=2, at_most=5, greedy=False))
... "(?:a){2,5}?"

cursive_re.group

Denotes a group whose contents can be retrieved after a match is performed.

Examples:

>>> str(group(text("a")))
... "(a)"

>>> str(group(any_of("abc"), name="chars"))
... "(?P<chars>[abc])"

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

cursive_re-0.0.2.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cursive_re-0.0.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file cursive_re-0.0.2.tar.gz.

File metadata

  • Download URL: cursive_re-0.0.2.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for cursive_re-0.0.2.tar.gz
Algorithm Hash digest
SHA256 63389ea84c09f06074ec7ab231512ab9fad3d2307a81447e67dd5900b05fd67d
MD5 9fc6dfd5d84d7873674dab9632853af4
BLAKE2b-256 5e3e26c23aecfe193ebbf89755e78d25487d94c47d0af508cf04782e30b36db2

See more details on using hashes here.

File details

Details for the file cursive_re-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: cursive_re-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for cursive_re-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c12166e4896464840c9e3c47fe37147fb2d595f43a600cb89106e7a7163516b8
MD5 360a26d2a64e1d1e0ae58848e7c07d64
BLAKE2b-256 16353df2e7b38ad43a300f9d6f93ea6ed8e8451288ef9a68939ed08ef5ce7aca

See more details on using hashes here.

Supported by

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