Repeating capture groups problem

John Long 21 Reputation points
2021-02-19T04:11:05.243+00:00

I need to parse strings like these string[] steelShapes = new string[] { "C100X11", "C100X8", "C10X15.3", "C10X20", "C12X20.7", "C150X16", "C15X40", "C150X19" }; I need to separate them into two strings, before and after the X. I know could split the strings at the X. However, I tried to write an regex expression to capture two groups. I have been able to capture either the first or the last part using these two separate expressions. patternOne = @"(C\d+(?=X))" patternTwo = @"(?:((?<=X)\d+)?|((?<=X)\d+.\d+))?$" What I can't do is to put them together to get the two groups. Can someone please explain how to put them together to make it work?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,101 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 118.4K Reputation points
    2021-02-19T07:03:03.203+00:00

    Check an example with a single string:

    string shape = "C100X11";
    
    Match m = Regex.Match( shape, @"^(C\d+)X(\d+(?:\.\d+)?)$" );
    if( m.Success )
    {
       string first_part = m.Groups[1].Value;
       string last_part = m.Groups[2].Value;
       . . .
    }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.