Package: re2shield
re2shieldre2shieldis a Python library that provides a shield for working with regular expressions using there2module.It allows you to compile and search for patterns in text using the powerful regular expression engine provided byre2.It allows you to hide the complexity of regular expressions and work with pattern identifiers instead.This project utilizes thegoogle/re2library, which is licensed under the BSD 3-Clause License. Please refer to theLICENSEfile ofgoogle/re2for more information.InstallationBefore installingre2shield, make sure thatre2is installed on your system. You can installre2by following the instructions on thegoogle/re2GitHub repository.Alternatively, you can use there2-installer.shscript located in thepackage/installeddirectory. This script automates the installation process forre2and its dependencies. Simply run the script using the following command:gitclonehttps://github.com/Npc-coder/re2shield.gitcdre2shield/installed
shre2-installer.shYou can installre2shieldusing pip:pipinstallre2shieldorgitclonehttps://github.com/Npc-coder/re2shield.gitcdre2shield
pipinstall.UpdatesVersion 0.2.1Bug FixRemoved flagAdded type and date fields to the Re2Shield class for additional metadata.Updated dump and load functions to include the new type and date fields when saving and loading the compiled patterns.Enhanced thestrmethod of the Re2Shield class to include the new fields in the output.Please refer to theUsagesection for examples of how to use these new features.UsageImporting the LibraryHere is a simple example demonstrating how to use re2shield:importre2shieldif__name__=="__main__":db=re2shield.Database(version='1.0.0')# Load patterns from filetry:db=re2shield.load('patterns.db')exceptFileNotFoundError:# If pattern file doesn't exist, compile the patternspatterns=[(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b',1),(r'\b\d{3}[-.\s]??\d{3}[-.\s]??\d{4}\b',2),(r'\d+',3)]expressions,ids=zip(*patterns)db.compile(expressions=expressions,ids=ids,overwrite=False)db.dump('patterns.db')# Find patterns in textdefmatch_handler(id,from_,to,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 CaseOne 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.FeaturesHide the complexity of regular expressionsWork with pattern identifiers instead of exposing the actual regular expression patternsCompile patterns for efficient matchingFind all occurrences of patterns in textCustomize the handling of matches using callback functionsLicenseThis project is licensed under the BSD 3-Clause License. See theLICENSEfile for details.