Skip to main content

A Python library that provides a convenient interface for compiling and matching regular expression patterns using the re2 library.

Project description

re2shield

re2shield is a Python library that provides a shield for working with regular expressions using the re2 module.

It allows you to compile and search for patterns in text using the powerful regular expression engine provided by re2.

It allows you to hide the complexity of regular expressions and work with pattern identifiers instead.

This project utilizes the google/re2 library, which is licensed under the BSD 3-Clause License. Please refer to the LICENSE file of google/re2 for more information.

Installation

Before installing re2shield, make sure that re2 is installed on your system. You can install re2 by following the instructions on the google/re2 GitHub repository.

Alternatively, you can use the re2-installer.sh script located in the package/installed directory. This script automates the installation process for re2 and its dependencies. Simply run the script using the following command:

bash package/installed/re2-installer.sh

You can install re2shield using pip:

pip install re2shield

or

git clone https://github.com/Npc-coder/re2shield.git
cd re2shield
pip install .

Usage

Importing the Library

Here is a simple example demonstrating how to use re2shield:

import re2shield

if __name__ == "__main__":
    db = re2shield.Database()

    # 파일에서 패턴 불러오기
    try:
        db = re2shield.load('patterns.pkl')
    except FileNotFoundError:
        # 패턴 파일이 없는 경우 패턴 컴파일
        patterns = [
            (r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', 1, None),
            (r'\b\d{3}[-.\s]??\d{3}[-.\s]??\d{4}\b', 2, None),
            (r'\d+', 3, None)
        ]

        expressions, ids, flags = zip(*patterns)
        db.compile(expressions=expressions, ids=ids, flags=flags)
        db.dump('patterns.pkl')

    # 텍스트에서 패턴 찾기
    def match_handler(id, from_, to, flags, context):
        print(f"Match found for pattern {id} from {from_} to {to}: {context}")

    db.scan('test@ex12ample12.com', match_handler)

In this example, we create a re2shield.Database object, compile a list of patterns with their corresponding identifiers, and then search for those patterns in the provided text.

The match_handler function is called for each match found, allowing you to process the matches as desired.

Use Case

One of the key advantages of re2shield is its ability to hide the actual regular expression patterns from users during distribution. By compiling the patterns with re2 and using pattern identifiers, you can distribute your code without exposing the underlying regular expression logic. This provides an additional layer of abstraction and enhances the security of your regular expression patterns.

License

This project is licensed under the BSD 3-Clause License. See the LICENSE file for details.

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

re2shield-0.1.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

re2shield-0.1.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file re2shield-0.1.1.tar.gz.

File metadata

  • Download URL: re2shield-0.1.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for re2shield-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9a0636a706dd04e1c1f7ed320a59d944877f54a57f73c631ee942b5edbcbad75
MD5 1e1aef4ac2ca1418fbb4eb41ea5c0ed0
BLAKE2b-256 652545a2af7a8a5771f90a18a4e9d6fb920f04364a33d83ac1f5fb7827ad06d3

See more details on using hashes here.

File details

Details for the file re2shield-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: re2shield-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for re2shield-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cfeec1d9f327223b1f156eca7cd42d1a65a0ee264a6c279e9fcfa739a8345abb
MD5 a908bf7daf3ceae1482416244455f278
BLAKE2b-256 e58ad2fc97df544dd4971e0e8c8fbee25c95dac000a14ddf91a000e45ccdb226

See more details on using hashes here.

Supported by

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