Regular Expression and backreferencing groups

Posted on 2007-07-19
Last Modified: 2010-04-15
I need help with a regular expression.

I have input strings like:
(DCS 1234)
(DCS 1234, 5678)
(DCS 1234, 5678, 9876)

I need to be able to match these strings and back-reference the numbers.

I have started with something like:

Regex reDCSList = new Regex(@"\(DCS ((,*\d{4})+).*");

but is only groups the first 1234, I never get the following numbers.

Any help would be appreciated.

Question by:aogusafa
    LVL 15

    Assisted Solution

    the way you have done the grouping means that it will look for one set of numbers as group 1, then include the rest of the string in the match but not as part of any group.

    this regex will match all of the groups of numbers into group 1,  then you will need to split it up by commas
    \(DCS ([\d{4},?\s?]+)\)

    to get an array of the numbers as strings:
    string s = match.Groups[1];
    string[] numbers = s.Replace(" ", "").Split(",") //this trims out the spaces and then splits it up at the commas
    LVL 84

    Accepted Solution

    @"\(DCS ((,*\s*\d{4})+).*"
    LVL 7

    Expert Comment

    Regex regex = new Regex(@"\(DCS(?: (?<number>\d{4}),)* (?<number>\d{4})\)");
    Group group = regex.Match("(DCS 1234, 5678, 9876)").Groups["number"];
    foreach(Capture capture in group.Captures)

    Author Comment

    Thanks Experts!

    I split the points up because Solar Flare showed me how to access the data once it had been matched, but I liked the elegance of ozo's RE.

    Works great.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now