First we need to start with a regular expression that matches the pattern we are looking for. This will be consistent with your example (if ABC is alphanumeric): \w+\s*=\s*\d+
Next, we need to determine what we want to fix in the match by defining capture groups .. Net includes support for the named capture groups, which I absolutely adore. We specify a group with (?<name for capture>expression) , turning our regular expression 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 ); }
source share