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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:10:08+00:00 2026-05-16T12:10:08+00:00

In .NET, regex is not organizing captures as I would expect. (I won’t call

  • 0

In .NET, regex is not organizing captures as I would expect. (I won’t call this a bug, because obviously someone intended it. However, it’s not how I’d expect it to work nor do I find it helpful.)

This regex is for recipe ingredients (simplified for sake of example):

(?<measurement>           # begin group
  \s*                     # optional beginning space or group separator
  (
    (?<integer>\d+)|      # integer
    (
      (?<numtor>\d+)      # numerator
      /
      (?<dentor>[1-9]\d*) # denominator. 0 not allowed
    )
  )
  \s(?<unit>[a-zA-Z]+)
)+                        # end group. can have multiple

My string: 3 tbsp 1/2 tsp

Resulting groups and captures:

[measurement][0]=3 tbsp
[measurement][1]= 1/2 tsp
[integer][0]=3
[numtor][0]=1
[dentor][0]=2
[unit][0]=tbsp
[unit][1]=tsp

Notice how even though 1/2 tsp is in the 2nd Capture, it’s parts are in [0] since these spots were previously unused.

Is there any way to get all of the parts to have predictable useful indexes without having to re-run each group through the regex again?

  • 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-16T12:10:08+00:00Added an answer on May 16, 2026 at 12:10 pm

    Is there any way to get all of the parts to have predictable useful indexes without having to re-run each group through the regex again?

    Not with Captures. And if you’re going to perform multiple matches anyway, I suggest you remove the + and match each component of the measurement separately, like so:

      string s = @"3 tbsp 1/2 tsp";
    
      Regex r = new Regex(@"\G\s* # anchor to end of previous match
        (?<measurement>           # begin group
          (
            (?<integer>\d+)       # integer
          |
            (
              (?<numtor>\d+)      # numerator
              /
              (?<dentor>[1-9]\d*) # denominator. 0 not allowed
            )
          )
          \s+(?<unit>[a-zA-Z]+)
        )                         # end group.
      ", RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture);
    
      foreach (Match m in r.Matches(s))
      {
        for (int i = 1; i < m.Groups.Count; i++)
        {
          Group g = m.Groups[i];
          if (g.Success)
          {
            Console.WriteLine("[{0}] = {1}", r.GroupNameFromNumber(i), g.Value);
          }
        }
        Console.WriteLine("");
      }
    

    output:

    [measurement] = 3 tbsp
    [integer] = 3
    [unit] = tbsp
    
    [measurement] = 1/2 tsp
    [numtor] = 1
    [dentor] = 2
    [unit] = tsp
    

    The \G at the beginning ensures that matches occur only at the point where the previous match ended (or at the beginning of the input if this is the first match attempt). You can also save the match-end position between calls, then use the two-argument Matches method to resume parsing at that same point (as if that were really the beginning of the input).

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

Sidebar

Related Questions

This is a continuation of my previous question .NET regex engine returns no matches
I'm performing regex matching in .NET against strings that look like this: 1;#Lists/General Discussion/Waffles
This is JavaScript regex. regex = /(http:\/\/[^\s]*)/g; text = I have http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd and I
I am not a regex ninja. I've been tweaking this for an hour, and
For some reason the following regex which does work in .NET is not working
I can't seem to figure out captures + groups in Regex (.net). Let's say
Mono's Regex implementation has a bug which means it does not handle regex character
I am not an expert but I am trying to write .NET regex expression
I 'borrowed' a regex from this website : http://daringfireball.net/2010/07/improved_regex_for_matching_urls that is almost complete but
I'm trying to use a .NET Regex to validate the input format of a

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.