Skip to content
 
 

Repository files navigation

Verbex: Python verbal based regular expressions

Generic badge Code style: black PyPI pyversions Generic badge Generic badge Generic badge Generic badge Dynamic TOML Badge

Installation

pip install Verbex

Notes

  • Verbex now depends on the regex package (instead of Python's built-in re engine) for faster and more expressive pattern evaluation.
  • Quantifier methods default to possessive mode for performance (maybe, zero_or_more, one_or_more, n_times, n_times_or_more, n_to_m_times, anything).
  • If you need legacy backtracking behavior, pass possessive=False.
  • You can set the default quantifier behavior once per expression with Verbex(default_possessive=False).
  • OR(...) alternates only against the most recently added fragment. Use or_expr(...) to alternate against the full expression.

Usage

from verbex import Verbex
verbex = Verbex()

Documentation

API

Examples

Testing if we have a valid URL

from verbex import Verbex

# Create an example of how to test for correctly formed URLs
verbex = Verbex()
tester = (verbex.
            start_of_line().
            find('http').
            maybe('s').
            find('://').
            maybe('www.').
            anything_but(' ').
            end_of_line()
)

# Create an example URL
test_url = "https://www.google.com"

# Test if the URL is valid
if tester.regex().fullmatch(test_url):
    print("Valid URL")

# Print the generated regex
print(tester)  # Example: ^http?+://(?:www\.)?+[^ ]+$

Replacing strings

from verbex import Verbex

# Create a test string
replace_me = "Replace bird with a duck"

# Create an expression that looks for the word "bird"
expression = Verbex().find('bird')

# Compile and use the regular expression
regexp = expression.regex()
result_re = regexp.sub('duck', replace_me)
print(result_re)

Developer setup : running the tests

just tests

Other implementations

You can view all implementations on VerbalExpressions.github.io

About

Python regular expressions made easy

Topics

Resources

Code of conduct

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages