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 6215729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:06:29+00:00 2026-05-24T07:06:29+00:00

Would it be possible to define a global flag so that Python’s re.compile() automatically

  • 0

Would it be possible to define a global flag so that Python’s re.compile() automatically sets it ? For instance I want to use re.DOTALL flag for all my RegExp in — say — a class?

It may sound weird at first, but I’m not really in control of this part of the code since it’s generated by YAPPS. I just give YAPPS a string containing a RegExp and it calls re.compile(). Alas, I need to use it in re.DOTALL mode.

A quick fix would be to edit the generated parser and add the proper option. But I still hope there’s another and more automated way to do this.

EDIT:
Python allows you to set flags with the (?…) construct, so in my case re.DOTALL is (?s). Though usefull, it doesn’t apply on a whole class, or on a file.

So my question still holds.

  • 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-24T07:06:30+00:00Added an answer on May 24, 2026 at 7:06 am

    Yes, you can change it to be globally re.DOTALL. But you shouldn’t. Global settings are a bad idea at the best of times — this could cause any Python code run by the same instance of Python to break.


    So, don’t do this:

    The way you can change it is to use the fact that the Python interpreter caches modules per instance, so that if somebody else imports the same module they get the object to which you also have access. So you could rebind re.compile to a proxy function that passes re.DOTALL.

    import re
    re.my_compile = re.compile
    re.compile = lambda pattern, flags: re.my_compile(pattern, flags | re.DOTALL)
    

    and this change will happen to everybody else.

    You can even package this up in a context manager, as follows:

    from contextlib import contextmanager
    
    @contextmanager
    def flag_regexen(flag):
        import re
        re.my_compile = re.compile
        re.compile = lambda pattern, flags: re.my_compile(pattern, flags | flag)
        yield
        re.compile = re.my_compile
    

    and then

    with flag_regexen(re.DOTALL):
        <do stuff with all regexes DOTALLed>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would it be possible to write a class that is virtually indistinguishable from an
Would it be possible to add a new operator to the String class that
Would it be possible to write a script that gave the user the ability
I would like to know whether it is possible to define own doxygen comment
is it possible to define a circular list in erlang? http://en.wikipedia.org/wiki/Linked_list first question would
Possible Duplicate: Global int variable objective c I would like to create a global
Is it possible to define in XML schema that there must be some certain
Is it possible to define a custom super global variable? (whether in code, or
Possible Duplicate: Ruby on Rails: Where to define global constants? I am interested in
I was wondering if it would be possible to retrieve the complete list of

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.