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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:10:25+00:00 2026-05-25T18:10:25+00:00

I have a regex ‘simple’ that I’d like to use as a building block

  • 0

I have a regex ‘simple’ that I’d like to use as a building block for another regex ‘complex’. The trouble is, the capture groups in ‘simple’ are interfering with ‘complex’. These low level capture groups are details I don’t to care about. I’d love to remove them before the regex is consumed.

The question is: how?

Put another way, in code, this isn’t working well:

simple = /(a)bc/
complex = /(#{simple}) - (#{simple})/
complex.match("abc - abc").captures # => ["abc", "a", "abc", "a"]
// when I need ["abc","abc"]

I’d much rather write:

simple = /(a)bc/
complex = /(#{simple.without_capture}) - (#{simple.without_capture})/
complex.match("abc - abc").captures # => ["abc", "abc"]

I’m a stuck on how to do this, but I’m betting it’s been done before. The implementation of Regex#without_capture would need to of course account for non-capturing groups, look ahead/behind, etc. So simply removing all the () isn’t enough. Also, finding the matching ) for capture groups seems a little challenging.

Thoughts?

EDIT: I forgot to mention. I don’t want to manually create two versions of simple (a capturing and non-capturing). In my actual case it would be impractical to maintain both versions. It’d be much better to be able to toggle the capturing dynamically.

  • 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-25T18:10:27+00:00Added an answer on May 25, 2026 at 6:10 pm

    This is harder than I thought. Rather than spin more wheels if I change one requirement everything seems easy. Instead of trying to replace any capture group, replace only named capture groups.

    Thanks @JustinMorgan and @TimPietzcker for getting me this far.

    This is what I’ve come up with:

    class Regexp
      # replaces all named capture groups with non-capturing groups
      # in other words, it replaces all (?<*>...) with (?:...)
      def without_named_captures
          named_captures = %r{\(\?<[^>]+>}
          pattern = self.source.gsub(named_captures, "(?:")
          Regexp.new(pattern)
      end
    end
    

    Which passes this spec:

    describe "Regexp Extensions" do
      describe "#without_named_captures" do
        it "should replace named captures with non-captures" do
          p1 = /(?<a>.*) - (?<b>.*)/
          p2 = p1.without_named_captures
    
          p2.should == /(?:.*) - (?:.*)/
    
          # sanity check
          p1.match('abc - def').should have_exactly(3).items
          p2.match('abc - def').should have_exactly(1).items
        end
      end
    end
    

    Dealing with recursion, escaping, and all the other junk, just goes away when the token is more complex than a single ‘(‘. If I use named captures everywhere, I can use this method. If I don’t, well things behave normally.

    It’s late, so I don’t know if I’m missing anything, but I think this’ll work.

    Thanks for the help everyone.

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

Sidebar

Related Questions

I have a regex that looks like this /^(?:\w+\s)*(\w+)$*/ What is the ?: ?
I have a regex that looks like this ((1010xxx)?(\d{11}|\d{10}|\d{7})+) Basically I want it to
I have a regex that is going to end up being a bit long
I have a regex call that I need help with. I haven't posted my
I have a RegEx that is working for me but I don't know WHY
I have simple regex \.*\ for me its says select everything between and ,
I have this regex thanks to another wonderful StackOverflow user /(?:-\d+)*/g I want it
I have some Regex, it looks like this: string regexForDrop = @^((%27)|'|(\-\-))\s*(d|%64|%44)(r|%72|%52)(o|%6F|%4F)(p|%70|%50)$; It works
Sigh, regex trouble again. I have following in $text : [img]http://www.site.com/logo.jpg[/img] and [url]http://www.site.com[/url] I
I have a regex issue that I need help with. Its trying to validate

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.