private static bool Match(string target, string pattern)
{
string expression = pattern;
// 1) Suppress unused regular expression characters with special meaning
// the following characters have special meaning in a regular expression []\^$.|?*+()
// the following characters are OPC UA wildcards %_\[]!
// The specail meaning of the regular expression characters not coincident with the
// OPC UA wildcards must be suppressed so as not to interfere with matching.
// preceed all '^', '$', '.', '|', '?', '*', '+', '(', ')' with a '\'
expression = Regex.Replace(expression, "([\\^\\$\\.\\|\\?\\*\\+\\(\\)])", "\\$1", RegexOptions.Compiled);
// 2) Replace all OPC UA wildcards with their regular expression equivalents
// replace all '%' with ".+", except "\%"
expression = Regex.Replace(expression, "(?<!\\\\)%", ".*", RegexOptions.Compiled);
// replace all '_' with '.', except "\_"
expression = Regex.Replace(expression, "(?<!\\\\)_", ".", RegexOptions.Compiled);
// replace all "[!" with "[^", except "\[!"
expression = Regex.Replace(expression, "(?<!\\\\)(\\[!)", "[^", RegexOptions.Compiled);
return Regex.IsMatch(target, expression);
}
#endregion