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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:47:25+00:00 2026-05-17T17:47:25+00:00

I can’t seem to figure out captures + groups in Regex (.net). Let’s say

  • 0

I can’t seem to figure out captures + groups in Regex (.net).

Let’s say I have the following input string, where each letter is actually a placeholder for more complex regex expression (so simple character exclusion won’t work):

CBDAEDBCEFBCD

Or, more generically, here is a string pattern written in ‘regex’:

(C|B|D)*A(E*)(D|B|C)*(E*)F(B|C|D)*

There will only be one A and one F.
I need to capture as individual ‘captures’ (or matches or groups) all instances of B, C, D (which in my app are more complex groups) that occur after A and before F. I also need A and F. I don’t need E. And I don’t need the C,B,D before the A or the B,C,D after the F.

I would expect the correct result to be:

Groups["start"] (1 capture) = A
Groups["content"] (3 captures)  
  Captures[0] = D  
  Captures[1] = B
  Captures[2] = C
Groups["end"] (1 capture) = F

I tried a few feeble attempts but none of them worked.

Only “incorrectly” captures the last C before EF in the sample string above (as well as correctly start = A, end = F)

(?<=(?<start>A)).+(?<content>B|C|D).+(?=(?<end>F))

Same results as above (just added a + after (?B|C|D) )

(?<=(?<start>A)).+(?<content>B|C|D)+.+(?=(?<end>F))

Got rid of look-around stuff… same result as above

(?<start>A).+(?<content>B|C|D)+.+(?<end>F)

And then my good-for-nothing brain went on strike.

So, what’s the right way to approach this? Are look-arounds really needed for this or not?

Thanks!

  • 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-17T17:47:26+00:00Added an answer on May 17, 2026 at 5:47 pm

    Yeah, forget the lookarounds, they just complicate things needlessly. But I suspect your final regex will work if you make that first .+ reluctant:

    (?<start>A).+?(?<content>B|C|D)+.+(?<end>F)
    

    EDIT: yep:

    string s = "CBDAEDBCEFBCD";
    Regex r = new Regex(@"(?<start>A).+?(?<content>B|C|D)+.+(?<end>F)");
    
    foreach (Match m in r.Matches(s))
    {
      Console.WriteLine(@"Groups[""start""] = {0}", m.Groups["start"]);
      foreach (Capture c in m.Groups["content"].Captures)
      {
        Console.WriteLine(@"Capture[""content""] = {0}", c.Value);
      }
      Console.WriteLine(@"Groups[""end""] = {0}", m.Groups["end"]);
    }
    

    output:

    Groups["start"] = A
    Capture["content"] = D
    Capture["content"] = B
    Capture["content"] = C
    Groups["end"] = F
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone tell me what's wrong with this robots.txt? http://bizup.cloudapp.net/robots.txt The following is the
Can the first char of a string be retrieved by doing the following? MyString.ToCharArray[0]
Can you cast a List<int> to List<string> somehow? I know I could loop through
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can a LINQ enabled app run on a machine that only has the .NET
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
Can I use the following across all browsers? <a href=# onclick=doSomething()>Click here.</a> Is this
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) I have a Rails

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.