4

I have a string of characters which has 0 or more occurrences of ABC = dddd within it. The dddd stands for an integer value, not necessarily four digits.

What I'd like to do is capture the integer values that occur within this pattern. I know how to perform matches with regexes but I'm new to capturing. It's not necessary to capture all the ABC integer values in one call—it's fine to loop over the string.

If this is too involved I'll just write a tiny parser, but I'd like to use regex if it's reasonably elegant. Expertise greatly appreciated.

2 Answers 2

3

First we need to start with a regex that matches the pattern we are looking for. This will match the example you have given (assuming ABC is alphanumeric): \w+\s*=\s*\d+

Next we need to define what we want to capture in a match by defining capture groups. .Net includes support for named capture groups, which I absolutely adore. We specify a group with (?<name for capture>expression), turning our regex into: (?<key>\w+)\s*=\s*(?<value>\d+). This gives us two captures, key and value.

Using this, we can iterate over all matches in your text:

Regex pattern = new Regex(@"(?<key>\w+)\s*=\s*(?<value>\d+)");
string body = "This is your text here.  value = 1234";
foreach (Match match in pattern.Matches(body))
{
    Console.WriteLine("Found key {0} with value {1}", 
        match.Groups.Item["key"].Value, 
        match.Groups.Item["value"].Value
    );
}
1

You can use something like this:

MatchCollection allMatchResults = null;
try {
    // This matches a literal '=' and then any number of digits following
    Regex regexObj = new Regex(@"=(\d+)");
    allMatchResults = regexObj.Matches(subjectString);
    if (allMatchResults.Count > 0) {
        // Access individual matches using allMatchResults.Item[]
    } else {
        // Match attempt failed
    } 
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}

Based on your coment, perhaps this is more what you're after:

try {
    Regex regexObj = new Regex(@"=(\d+)");
    Match matchResults = regexObj.Match(subjectString);
    while (matchResults.Success) {
        for (int i = 1; i < matchResults.Groups.Count; i++) {
            Group groupObj = matchResults.Groups[i];
            if (groupObj.Success) {
                // matched text: groupObj.Value
                // match start: groupObj.Index
                // match length: groupObj.Length
            } 
        }
        matchResults = matchResults.NextMatch();
    } 
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}
1
  • Thanks, but I know how to do regular matches (see question); I'd like to use the capturing capability of .NET regex. Commented Mar 29, 2011 at 2:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.