Opc.Ua.ContentFilter.Match C# (CSharp) Method

Match() private static method

Returns true if the target string matches the UA pattern string. The pattern string may include UA wildcards %_\[]!
private static Match ( string target, string pattern ) : bool
target string String to check for a pattern match.
pattern string Pattern to match with the target string.
return bool
        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