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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:11:50+00:00 2026-06-14T23:11:50+00:00

Ruby’s regular expressions have a feature called atomic grouping (?>regexp) , described here ,

  • 0

Ruby’s regular expressions have a feature called atomic grouping (?>regexp), described here, is there any equivalent in Python’s re module?

  • 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-06-14T23:11:52+00:00Added an answer on June 14, 2026 at 11:11 pm

    Python does not directly support this feature, but you can emulate it by using a zero-width lookahead assert ((?=RE)), which matches from the current point with the same semantics you want, putting a named group ((?P<name>RE)) inside the lookahead, and then using a named backreference ((?P=name)) to match exactly whatever the zero-width assertion matched. Combined together, this gives you the same semantics, at the cost of creating an additional matching group, and a lot of syntax.

    For example, the link you provided gives the Ruby example of

    /"(?>.*)"/.match('"Quote"') #=> nil
    

    We can emulate that in Python as such:

    re.search(r'"(?=(?P<tmp>.*))(?P=tmp)"', '"Quote"') # => None
    

    We can show that I’m doing something useful and not just spewing line noise, because if we change it so that the inner group doesn’t eat the final ", it still matches:

    re.search(r'"(?=(?P<tmp>[A-Za-z]*))(?P=tmp)"', '"Quote"').groupdict()
    # => {'tmp': 'Quote'}
    

    You can also use anonymous groups and numeric backreferences, but this gets awfully full of line-noise:

    re.search(r'"(?=(.*))\1"', '"Quote"') # => None
    

    (Full disclosure: I learned this trick from perl’s perlre documentation, which mentions it under the documentation for (?>...).)

    In addition to having the right semantics, this also has the appropriate performance properties. If we port an example out of perlre:

    [nelhage@anarchique:~/tmp]$ cat re.py
    import re
    import timeit
    
    
    re_1 = re.compile(r'''\(
                               (
                                 [^()]+           # x+
                               |
                                 \( [^()]* \)
                               )+
                           \)
                       ''', re.X)
    re_2 = re.compile(r'''\(
                               (
                                 (?=(?P<tmp>[^()]+ ))(?P=tmp) # Emulate (?> x+)
                               |
                                 \( [^()]* \)
                               )+
                           \)''', re.X)
    
    print timeit.timeit("re_1.search('((()' + 'a' * 25)",
                        setup  = "from __main__ import re_1",
                        number = 10)
    
    print timeit.timeit("re_2.search('((()' + 'a' * 25)",
                        setup  = "from __main__ import re_2",
                        number = 10)
    

    We see a dramatic improvement:

    [nelhage@anarchique:~/tmp]$ python re.py
    96.0800571442
    7.41481781006e-05
    

    Which only gets more dramatic as we extend the length of the search string.

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

Sidebar

Related Questions

In Ruby, I want to have a regex match either of two expressions with
(ruby noob here..apologies if I'm not asking the question correctly) So I have two
Ruby doesn't seem to have a facility for defining a protected/private block like so:
Ruby is preinstalled on my Mac and so I wanted to have a look
ruby-mode from svn, looks equal to 1.1 version here is emacs indentation of hash
Ruby has two different exceptions mechanisms: Throw/Catch and Raise/Rescue. Why do we have two?
Ruby's standard popen3 module does not work on Windows. Is there a maintained replacement
Ruby newbie here. I'm following the Rails Tutorial and got stuck around 5.3 re:
Ruby Version: 1.8 Rails Version: 2.3 I have a class in which I have
Ruby has this great abstraction layer on top of Selenium called Capybara, which you

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.