Lightning-Fast Python Regex (flpc)
flpc is a powerful Python library that wraps the blazing-fast Rust regex crate, bringing enhanced speed to your regular expression operations. It is designed to be a drop-in replacement for Python's native `re` module, with some minor syntax differences. Currently at version 0.2.5, it is in an experimental stage, meaning its code structure and dependencies may change, and users should be prepared for manual migrations to newer versions.
Warnings
- breaking The `match()` function from the standard `re` module is renamed to `fmatch()` in `flpc` to avoid conflict with Python's `match` keyword. Direct calls to `flpc.match()` will fail.
- breaking When accessing captured groups from a match object using `group()`, an index must always be provided (e.g., `group(0)` for the entire match, `group(1)` for the first captured group). Calling `group()` without an argument is not supported.
- gotcha The `flpc` library is currently in an experimental stage. Its code structure and internal dependencies are subject to change, which may necessitate manual migrations for your project with future updates.
- gotcha Always check if a match object is returned (i.e., not `None`) before attempting to access its methods like `group()`, `start()`, or `end()`. Accessing methods on a `None` object will result in an `AttributeError`.
- gotcha While `flpc` aims to mirror the `re` module's API, it is not a complete, drop-in replacement. Minor behavioral differences or unimplemented functionalities compared to the standard `re` module might exist.
Install
-
pip install flpc
Imports
- flpc
import flpc
- fmatch
flpc.fmatch(pattern, string)
Quickstart
import flpc
# Using flpc.search
text = "Hello world, hello Rust!"
pattern = r"hello (\w+)"
match_obj_search = flpc.search(pattern, text, flpc.IGNORECASE)
if match_obj_search:
print(f"Search found: {match_obj_search.group(0)}") # group(0) for entire match
print(f"Captured group: {match_obj_search.group(1)}")
else:
print("No match found by search.")
# Using flpc.fmatch (equivalent to re.match, matches only at the beginning)
text_start = "Hello Python, hello flpc!"
pattern_start = r"Hello (\w+)"
match_obj_fmatch = flpc.fmatch(pattern_start, text_start)
if match_obj_fmatch:
print(f"fmatch found: {match_obj_fmatch.group(0)}")
print(f"Captured group: {match_obj_fmatch.group(1)}")
else:
print("No match found by fmatch.")
# Compile a pattern for efficiency
compiled_pattern = flpc.compile(r"rust", flpc.IGNORECASE)
match_compiled = compiled_pattern.search(text)
if match_compiled:
print(f"Compiled pattern search found: {match_compiled.group(0)}")