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

  • Home
  • SEARCH
  • 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 9160289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:42:12+00:00 2026-06-17T13:42:12+00:00

I have gone through the docs for Atomic Grouping and rubyinfo and some questions

  • 0

I have gone through the docs for Atomic Grouping and rubyinfo and some questions came into my mind:

  1. Why the name "Atomic grouping"? What "atomicity" does it have that general grouping doesn’t?
  2. How does atomic grouping differ to general grouping?
  3. Why are atomic groups called non-capturing groups?

I tried the below code to understand but had confusion about the output and how differently they work on the same string as well?

irb(main):001:0> /a(?>bc|b)c/ =~ "abbcdabcc"
=> 5
irb(main):004:0> $~
=> #<MatchData "abcc">
irb(main):005:0> /a(bc|b)c/ =~ "abcdabcc"
=> 0
irb(main):006:0> $~
=> #<MatchData "abc" 1:"b">
  • 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-17T13:42:13+00:00Added an answer on June 17, 2026 at 1:42 pm

    A () has some properties (include those such as (?!pattern), (?=pattern), etc. and the plain (pattern)), but the common property between all of them is grouping, which makes the arbitrary pattern a single unit (unit is my own terminology), which is useful in repetition.

    The normal capturing (pattern) has the property of capturing and group. Capturing means that the text matches the pattern inside will be captured so that you can use it with back-reference, in matching or replacement. The non-capturing group (?:pattern) doesn’t have the capturing property, so it will save a bit of space and speed up a bit compared to (pattern) since it doesn’t store the start and end index of the string matching the pattern inside.

    Atomic grouping (?>pattern) also has the non-capturing property, so the position of the text matched inside will not be captured.

    Atomic grouping adds property of atomic compared to capturing or non-capturing group. Atomic here means: at the current position, find the first sequence (first is defined by how the engine matches according to the pattern given) that matches the pattern inside atomic grouping and hold on to it (so backtracking is disallowed).

    A group without atomicity will allow backtracking – it will still find the first sequence, then if the matching ahead fails, it will backtrack and find the next sequence, until a match for the entire regex expression is found or all possibilities are exhausted.

    Example

    Input string: bbabbbabbbbc
    Pattern: /(?>.*)c/

    The first match by .* is bbabbbabbbbc due to the greedy quantifier *. It will hold on to this match, disallowing c from matching. The matcher will retry at the next position to the end of the string, and the same thing happens. So nothing matches the regex at all.


    Input string: bbabbbabbbbc
    Pattern: /((?>.*)|b*)[ac]/, for testing /(((?>.*))|(b*))[ac]/

    There are 3 matches to this regex, which are bba, bbba, bbbbc. If you use the 2nd regex, which is the same but with capturing groups added for debugging purpose, you can see that all the matches are result of matching b* inside.

    You can see the backtracking behavior here.

    • Without the atomic grouping /(.*|b*)[ac]/, the string will have a single match which is the whole string, due to backtracking at the end to match [ac]. Note that the engine will go back to .* to backtrack by 1 character since it still have other possibilities.

      Pattern: /(.*|b*)[ac]/
      bbabbbabbbbc
      ^             -- Start matching. Look at first item in alternation: .*
      bbabbbabbbbc
                  ^ -- First match of .*, due to greedy quantifier
      bbabbbabbbbc
                  X -- [ac] cannot match
                    -- Backtrack to ()      
      bbabbbabbbbc
                 ^  -- Continue explore other possibility with .*
                    -- Step back 1 character
      bbabbbabbbbc
                  ^ -- [ac] matches, end of regex, a match is found
      
    • With the atomic grouping, all possibilities of .* is cut off and limited to the first match. So after greedily eating the whole string and fail to match, the engine have to go for the b* pattern, where it successfully finds a match to the regex.

      Pattern: /((?>.*)|b*)[ac]/
      bbabbbabbbbc
      ^             -- Start matching. Look at first item in alternation: (?>.*)
      bbabbbabbbbc
                  ^ -- First match of .*, due to greedy quantifier
                    -- The atomic grouping will disallow .* to be backtracked and rematched
      bbabbbabbbbc
                  X -- [ac] cannot match
                    -- Backtrack to ()
                    -- (?>.*) is atomic, check the next possibility by alternation: b*
      bbabbbabbbbc
      ^             -- Starting to rematch with b*
      bbabbbabbbbc
        ^           -- First match with b*, due to greedy quantifier
      bbabbbabbbbc
         ^          -- [ac] matches, end of regex, a match is found
      

      The subsequent matches will continue on from here.

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

Sidebar

Related Questions

I have gone through a couple of similar questions but could reach a definite
I have gone through different questions/articles on Message Brokers and ESBs(Even on stackoverflow). Still
Have gone through similar questions here, but have not been able to get this
while reading cocoa fundamental guide i have gone through: (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender
I have gone through similar questions on Stackoverflow but still can't get a good
I have gone through tons of the form_for nested resource questions and can't get
I have gone through Apple docs about UITableView class and delegate reference but couldn't
I am using the MVC architecture (i have gone through the docs on MVC,
I'm newbie to android. I have gone through the android docs and I googled
Has anyone integrated google docs into their iOS app? Having gone through the example

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.