Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3944432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:53:59+00:00 2026-05-20T00:53:59+00:00

I’m trying to load a list of regex patterns from a file, and I’d

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:54:00+00:00Added an answer on May 20, 2026 at 12:54 am

    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.py like this:

    mappings = [
        # (regex, action_name)
        (r'Hi ([a-z]+)!', greet),
        (r'Bye', bye),
    ]
    
    def greet(msg):
        return 'Hello'
    
    def bye(msg):
        return 'Bye'
    

    In your main.py you import the mappings, compile all regex expressions (one-time operation to avoid recompiling them every search) and then do a search against incoming messages.

    import rules
    
    # compile regex
    compiled = dict([(re.compile(regex), action_name) for (regex, action_name) in rules.mappings])
    
    def incoming(msg):
        for regex, action_name in compiled:
            if regex.search(msg):
                # if search is successfull, execute action
                action_function = getattr(rules, action_name)
                action(msg)
    

    Of course it depends on how you want to trigger that action, but I hope the main principle is clear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.