Somewhat related, I created a worksheet function to check for regex matches. It takes a string and a regex pattern as input, and returns TRUE if the string matches the pattern.
[ExcelFunction]
public static object RegexMatch(
[ExcelArgument(Name = "Text")] string text,
[ExcelArgument(Name = "Regex")] string pattern,
[ExcelArgument(Name = "[Ignore Case]")] object ignoreCase) {
object arg;
if (!Optional.TryGet(ignoreCase, true, out arg)) return arg;
var rOptions = (bool)arg
? RegexOptions.None
: RegexOptions.IgnoreCase);
return Regex.IsMatch(text, pattern, rOptions);
} public static bool TryGet(object o, bool defaultValue, out object result) {
if (o is ExcelError) {
result = o;
return false;
}
else if (o is ExcelMissing) {
result = defaultValue;
return true;
}
else {
try {
result = Convert.ToBoolean(o);
return true;
}
catch (FormatException) {
result = ExcelError.ExcelErrorValue;
return false;
}
}
}