System.IO.SearchPattern.IsMatch C# (CSharp) Méthode

IsMatch() public méthode

public IsMatch ( string text ) : bool
text string
Résultat bool
		public bool IsMatch (string text)
		{
			return Match (ops, text, 0);
		}

Usage Example

Exemple #1
0
		/// <summary>
		/// Returns true if the string matches the pattern which may contain * and ? wildcards.
		/// </summary>
		/// <param name="pattern">Pattern to match.</param>
		/// <param name="fileName">Filename to match.</param>
		/// <param name="caseSensitive">If the match is case sensitive or not.</param>
		/// <returns>True if the patterna and the fileName match, false if not.</returns>
		/// <remarks>
		/// Based on robagar C# port of Jack Handy Codeproject article:
		/// http://www.codeproject.com/string/wildcmp.asp#xx1000279xx
		/// </remarks>
		private bool Match(string pattern, string fileName, bool caseSensitive)
		{
			// if not concerned about case, convert both string and pattern
			// to lower case for comparison
			if (!caseSensitive)
			{
				pattern = pattern.ToLower();
				fileName = fileName.ToLower();
			}            		
            if(string.IsNullOrEmpty(pattern))return false;//如果空字符 返回没有
            if (pattern.CompareTo("*")==0 || pattern.CompareTo("*.*")==0) {
                return true;//快速匹配
            }           
            // if pattern doesn't actually contain any wildcards, use simple equality
            //if (pattern.IndexOfAny(Wildcards) == -1)//不存在匹配符号
            //	return (fileName == pattern);
            bool findP = false;
            foreach (var v1 in Wildcards) {
                foreach (var v2 in pattern) {
                    if (v1.CompareTo(v2) == 0) {
                        findP = true; break;
                    }
                }
            }
            if (!findP) return (fileName == pattern);
			// otherwise do pattern matching
            SearchPattern mSearchPattern =new SearchPattern(pattern);
            return mSearchPattern.IsMatch(fileName);
            //下面逻辑可能有点问题
			int i = 0;
			int j = 0;
			while (i < fileName.Length && j < pattern.Length && pattern[j] != '*')
			{
				if ((pattern[j] != fileName[i]) && (pattern[j] != '?'))
				{
					return false;
				}
				i++;
				j++;
			}

			// if we have reached the end of the pattern without finding a * wildcard,
			// the match must fail if the string is longer or shorter than the pattern
			if (j == pattern.Length)
				return fileName.Length == pattern.Length;

			int cp = 0;
			int mp = 0;
			while (i < fileName.Length)
			{
				if (j < pattern.Length && pattern[j] == '*')
				{
					if ((j++) >= pattern.Length)
					{
						return true;
					}
					mp = j;
					cp = i + 1;
				}
				else if (j < pattern.Length && (pattern[j] == fileName[i] || pattern[j] == '?'))
				{
					j++;
					i++;
				}
				else
				{
					j = mp;
					i = cp++;
				}
			}

			while (j < pattern.Length && pattern[j] == '*')
			{
				j++;
			}

			return j >= pattern.Length;
		}