Regex match and substitute?
Given an input string, what is the most efficient way of checking for a
regex (say, in order), replacing it with another string and returning a
string array {MODIFIED STRING, REPLACED TEXT}?
I am currently doing this:
if (Regex.Match(query, @"\d+", RegexOptions.IgnoreCase).Success)
{
return System.Text.RegularExpressions.Regex.Replace(str, "(\d+)", "
NUMERIC", System.Text.RegularExpressions.RegexOptions.Compiled);
}
else if (Regex.Match(query, @"true|false", RegexOptions.IgnoreCase).Success)
{
return System.Text.RegularExpressions.Regex.Replace(str,
"(true|false)", " BOOLEAN",
System.Text.RegularExpressions.RegexOptions.Compiled);
}
But this seems like an overkill to me if I need to match a lot of regexes.
Any suggestions?
No comments:
Post a Comment