I’m trying to load a list of regex patterns from a file, and I’d like it to pull each regex entry into an array which can later be used to match incoming text patterns, then trigger an action based on them.
I’ve already gotten the hang of making re.search() work, but how can I load the regex patterns from a file, then subsequently scan the various regex patterns pulled from the file with the re.search() argument?
If you want to trigger an action when a regex matches an text pattern, you probably need some mapping of regex to the action (let’s assume it is a reference to a function defined in the same file as the mappings). Say we define the rules in a python file
rules.pylike this:In your
main.pyyou import the mappings, compile all regex expressions (one-time operation to avoid recompiling them every search) and then do a search against incoming messages.Of course it depends on how you want to trigger that action, but I hope the main principle is clear.