Reputation: 5146
I have a number of regular expressions that attempt to find items in a very long (~100 pages of text) string, with each looking for a particular thing (i.e. one looks for one type of information, another looks for a different type, etc).
Each regex group will have ~50 matches or so in the text, but it takes a while to run them all in sequence.
Is there a way that I can run all the of regular expressions on a string at once, or otherwise create a regular expression where the capture groups can occur in any order (any number of times)?
Upvotes: 1
Views: 84
Reputation: 336128
You can put all the single regexes into a large regex using groups and alternation:
bigregex = "(?:" + regex1 + "|" + regex2 + "|" + regex3 + ")"
However, this will only give you the desired result if the matches of each regex can never overlap, and if you're not using numbered capturing groups with backreferences.
Upvotes: 2
Reputation: 27561
This is probably not exactly what you need, but may spark an idea. I use this as a single pass, multi-token, find and replace.
public static string MultiReplace(this string target, Dictionary<string, string> replacementDictionary)
{
return Regex.Replace(target, "(" + String.Join("|", replacementDictionary.Keys.ToArray()) + ")", match => replacementDictionary[match.Value]);
}
Upvotes: 0