Skip to main content

REfO

Lacking a proper name, REfO stands for "Regular Expressions for Objects".

It's a python library that supplies a functionality very similar to the python re module (regular expressions) but for arbitrary sequences of objects instead of strings (sequences of characters).

In addition to that, it's possible to match each object in a sequence with not only equality, but an arbitrary python function. For example, if you have a sequence of integers you can make a regular expression that asks for a even number followed by a prime number followed by a 3-divisible number.

This software was written by Rafael Carrascosa while working at Machinalis in the first months of 2012.

Contact: rcarrascosa@machinalis.com or rafacarrascosa xyz gmail.com (replace " xyz " with "@")

Build Status

How to use it

The syntax is a little bit different than python's re, and similar to that of pyparsing, you have to more-or-less explicitly build the syntax tree of your regular expression. For instance:

"ab" is Literal("a") + Literal("b")

"a*" is Star(Literal("a"))

"(ab)+|(bb)*?" is:

a = Literal("a")
b = Literal("b")
regex = Plus(a + b) | Star(b + b, greedy=False)

You can also assign a group to any sub-match and later on retrieve the matched content, for instance:

regex = Group(Plus(a + b), "foobar")  | (b + b)
m = match(regex, "abab")
print m.span("foobar")  # prints (0, 4)

For more, check out the examples in the examples folder.

How we use it

At Machinalis we use REfO for applications similar to that in examples/words.py, check it out!

About the implementation

I use a Thompson-like virtual machine aproach, which ensures polynomial time worst-case complexity. See examples/poly_time.py for an example of this.

The implementation is heavily based on Russ Cox notes, see http://swtch.com/~rsc/regexp/regexp2.html for the source.

If you go to read the code, some glossary:

  • RE -- regular expression
  • VM -- virtual machine
  • Epsilon transitions -- All VM instructions that do not consume a symbol or stop the thread (for example an Accept).

Acknowledgements

Thanks Russ Cox for sharing the awesome info and insights on your web site.

Thanks Javier Mansilla for reviewing the code and being enthusiastic about it.

Thanks Machinalis for everything :)

Release files for FenixRefO 0.13.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for FenixRefO 0.13.1
File Size Uploaded
fenixrefo-0.13.1.tar.gz 8.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for FenixRefO 0.13.1
File Interpreter ABI Platform
fenixrefo-0.13.1-py3-none-any.whl Python 3 none any Details

Total release size: 18.7 kB

Release files / fenixrefo-0.13.1.tar.gz

Download URL fenixrefo-0.13.1.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6e9de2efcd5525b3600d71ce1bbe979f9877f527973a2e585cb63d75b40d16aa
BLAKE2b-256 checksum
How to use checksums
24cc6766ae549212657f160273d598e0d5640332f4c905b65bcae4afaa674279
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Darwin/21.6.0

Release files / fenixrefo-0.13.1-py3-none-any.whl

Download URL fenixrefo-0.13.1-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cbacd8d67eaf9df005b3a0c152aaf54eba16a4d86e57e179acb6a791ad83ba6a
BLAKE2b-256 checksum
How to use checksums
fff3657c7a62ad5bcd75268c112303586f501a2d5503a46302a19fcf314392ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Darwin/21.6.0

Release history Release notifications | RSS feed

This release

0.13.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page