• Stars
    star
    353
  • Rank 120,322 (Top 3 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created about 6 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Readable regular expressions for Python 3.6 and up.

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(r"\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("ab"))
'[^ab]'

>>> 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("a"))
'(?:a)*'

>>> str(zero_or_more(text("a")))
'(?:a)*'

>>> str(zero_or_more(text("abc")))
'(?:abc)*'

>>> 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("a"))
'(?:a)+'

>>> 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("abc"))
'(?:abc)?'

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

>>> str(maybe(group(text("abc"))))
'(abc)?'

>>> str(maybe(any_of("abc")))
'[abc]?'

cursive_re.repeated

Matches an expr repeated an exact number of times.

Examples:

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

>>> 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])'

More Repositories

1

dramatiq

A fast and reliable background task processing library for Python 3.
Python
3,640
star
2

awesome-advent-of-code

A collection of awesome resources related to the yearly Advent of Code challenge.
2,827
star
3

molten

A minimal, extensible, fast and productive framework for building HTTP APIs with Python 3.6 and later.
Python
982
star
4

django_dramatiq

A Django app that integrates with Dramatiq.
Python
347
star
5

dramatiq_dashboard

A dashboard for dramatiq, specific to its Redis broker.
Python
143
star
6

elm-ast

A parser for Elm in Elm.
Elm
143
star
7

racket-gui-easy

Declarative GUIs in Racket.
Racket
129
star
8

koyo

A web development toolkit for Racket.
Racket
129
star
9

web-app-from-scratch

Supporting material for my blog post series on writing a web application from scratch in Python.
Python
128
star
10

remember

Stash distractions away for later.
Swift
107
star
11

Franz

A desktop client for Apache Kafka.
Racket
106
star
12

browser-connect.vim

Live browser interaction for VIM.
Vim Script
102
star
13

marionette

A Racket library that lets you control Firefox via the Marionette Protocol.
Racket
73
star
14

deta

A database mapper for Racket.
Racket
60
star
15

threadop

Adds a threading operator to Python.
Python
55
star
16

anom-py

An ndb-like object mapper for Google Cloud Datastore.
Python
53
star
17

racket-lua

A #lang implementation of Lua 5.4 for Racket.
Racket
52
star
18

setup-racket

A GH action for installing Racket.
JavaScript
49
star
19

neko

A tiny kitten follows your mouse on macOS.
Swift
48
star
20

dramatiq_sqs

A Dramatiq broker that can be used with Amazon SQS.
Python
47
star
21

racket-http-easy

A high-level HTTP client for Racket.
Racket
43
star
22

rbrepl.vim

VIM plugin that allows you to run a Ruby REPL inside a VIM buffer.
Vim Script
41
star
23

racket-review

A linter for Racket.
Racket
41
star
24

racket-wasm

Wasm tooling in Racket.
Racket
39
star
25

nemea

Privacy focused website analytics.
Racket
35
star
26

racket-redis

Fast, idiomatic Redis bidings for Racket.
Racket
31
star
27

flask_dramatiq_example

An example app demonstrating how you can use Dramatiq with Flask.
Python
29
star
28

django_dramatiq_example

An example app demonstrating django_dramatiq.
Python
28
star
29

rackcheck

A property-based testing library for Racket.
Racket
28
star
30

taskqueues.com

A list of distributed task queueing software.
HTML
26
star
31

racket-chief

Chief runs Procfile-based applications. Like foreman, but written in Racket.
Racket
25
star
32

pyrepl.vim

VIM plugin that provides a way to run a Python REPL inside a VIM buffer.
Vim Script
25
star
33

hebi

A Snake-style game in Racket.
Racket
24
star
34

hugs

Hugs lets you map SQL expressions to Python functions.
Python
24
star
35

Noise

A Swift wrapper around Racket CS.
Racket
23
star
36

quicksilver.vim

Quicksilver is a VIM plugin whose purpose is to quicken the process of opening files from inside VIM.
Vim Script
22
star
37

racket-dbg

A server, client and UI for remotely debugging Racket applications.
Racket
19
star
38

racket-north

A database migration tool written in Racket.
Racket
18
star
39

racket-kafka

A Kafka client for Racket.
Racket
17
star
40

racket-smtp-server

An SMTP server implementation for Racket.
Racket
17
star
41

browser-connect-server

Live browser interaction for <INSERT-EDITOR-NAME-HERE>.
Scala
13
star
42

try-racket

An online playground for Racket.
Racket
13
star
43

racket-crontab

A cron-like scheduler for Racket.
Racket
12
star
44

racksnaps

Daily snapshots of the Racket Package Catalog.
Racket
12
star
45

racket-gui-extra

Platform-specific GUI controls for Racket.
Racket
11
star
46

racket-binfmt

A binary format parser generator DSL with support for limited context-sensitivity.
Racket
11
star
47

defn.io

My personal website.
Racket
10
star
48

.emacs.d

My Emacs configuration.
Emacs Lisp
10
star
49

racket-lz4

A pure-Racket decompressor for LZ4 data.
Racket
10
star
50

apistar_prometheus

Prometheus metrics for API Star apps.
Python
10
star
51

elm-querystring

A library for working with query strings in Elm.
Elm
8
star
52

apistar_dramatiq

Dramatiq integration for API Star apps.
Python
8
star
53

racket-sentry

A Sentry SDK for Racket.
Racket
8
star
54

koyo-shorty

Associated code for the "Racket Web Development with Koyo" screencast.
Racket
8
star
55

racket-protocol-buffers

A Protocol Buffers implementation for Racket.
Racket
7
star
56

h2p

A Python package that converts web pages to PDFs.
Python
7
star
57

falcon_sugar

A little sugar for Falcon applications.
Python
7
star
58

racket-forms

Web form validation for Racket.
Racket
7
star
59

resource_pool

A generic thread-safe resource pool for Python.
Python
7
star
60

elm-generate

Lazy list manipulation for Elm.
Elm
7
star
61

markii

A development-mode error handler for Python web applications.
Python
6
star
62

fargate_scraper

A CLI tool that scrapes AWS Fargate tasks to find Prometheus targets.
Python
6
star
63

dotfiles

Various configuration files from my setup.
Shell
6
star
64

molten_cookiecutter

A cookiecutter for motlen apps.
Python
6
star
65

modviz

Python module dependency visualizer.
Python
6
star
66

ido-clever-match

An alternative matcher for ido-mode.
Emacs Lisp
6
star
67

personal-website

My personal website.
Racket
5
star
68

racketcon2021-talk

Racket
5
star
69

racket-component

Application lifecycle management for Racket.
Racket
5
star
70

aoc2020

My Advent of Code 2020 solutions.
Racket
5
star
71

racket-actions-example

An example of testing Racket code using GitHub actions.
Racket
5
star
72

racketfest2023-talk

A talk I gave at RacketFest 2023.
Racket
5
star
73

apistar_sentry

A Sentry component for API Star.
Python
5
star
74

racket-import-profiler

A command line utility for profiling module dependency import times.
Racket
5
star
75

racket-net-ip

Utilities for working with IP addresses in Racket.
Racket
5
star
76

setup-racket-cache-example

An example repo that demonstrates how setup-racket packages can be cached with GHA.
4
star
77

aoc2021

My solutions to Advent of Code 2021.
Racket
4
star
78

tcopy

Tail call optimization for Python.
Python
4
star
79

racket-json-lexer

This package provides an implementation of a JSON lexer and pretty printer for Racket.
Racket
4
star
80

elm-cookiecutter

A cookiecutter template for Elm projects.
Elm
4
star
81

racket-geoip

Geolocation for Racket based on MaxMind's GeoIP databases.
Racket
4
star
82

ymage

ymage is a simple image slideshow creator written in Python
Python
4
star
83

watchdog_gevent

A gevent-based observer for watchdog.
Python
4
star
84

racket-messagepack

An implementation of the MessagePack serialization format for Racket.
Racket
4
star
85

trio-redis

A Redis client based on Trio.
Python
4
star
86

racket-net-mime-type

Utilities for working with MIME types in Racket.
Racket
4
star
87

apistar_cors

CORS support for API Star.
Python
4
star
88

racket-avro

A Racket implementation of the Apache Avro serialization format.
Racket
4
star
89

racket-bench-ws-logging

Racket
3
star
90

ruse

Scheme
3
star
91

racket-box-extra

Extra utilities for working with boxes in Racket.
Racket
3
star
92

racket-libsass

This package distributes libsass as a Racket package for Linux, MacOS and Windows.
Shell
3
star
93

pirate

A simple TPB CLI magnet link utility.
Python
3
star
94

cedar

A web service definition format and source code generator.
Python
3
star
95

racket-resource-pool

A generic blocking resource pool implementation for Racket.
Racket
3
star
96

py-test.el

A test runner for Python code.
Emacs Lisp
3
star
97

racket-libsqlite3

This package distributes libsqlite3 as a Racket package for Linux and macOS.
Shell
3
star
98

koyo-sentry

Integrates racket-sentry with koyo.
Racket
3
star
99

apistar_request_id

Request id generation and propagation for API Star.
Python
3
star
100

racket-ulid

Universally unique lexicographically sortable identifiers for Racket.
Racket
2
star