Source/NFX/Parsing/Utils.cs
/// <summary>
/// Returns true if supplied string matches pattern that can contain up to one * wildcard and multiple ? wildcards
/// </summary>
Returns true if supplied string matches pattern that can contain up to one * wildcard and multiple . wildcards
Returns true if supplied string matches pattern that can contain up to one * wildcard and multiple . wildcards
public static bool MatchPattern(this string str,     
                                         string pattern, 
                                         char wc ='*',
                                         char wsc ='?',
                                         bool senseCase = false)
    {
       if (str.IsNullOrWhiteSpace() && pattern.IsNullOrWhiteSpace()) return true;
       if (pattern.IsNullOrWhiteSpace()) return false;
       var iwc = pattern.IndexOf(wc);
       if (iwc>=0)
       {
          var iwc2 = pattern.LastIndexOf(wc);
          if (iwc2>iwc)
            throw new NFXException(StringConsts.TEXT_PATTERN_MULTI_WC_ERROR.Args(pattern, wc));
       }
       int istr=0, ipat=0;
       for(; istr<str.Length && ipat<pattern.Length; istr++,ipat++)
       {
          var pc = pattern[ipat];
          if (pc==wsc)
            continue;
          if (pc==wc)
          {
            var leftcnt = pattern.Length-1-ipat;
            if (leftcnt<=0) return true;
            if (istr>=str.Length-1) return false;
            pattern = pattern.Substring(ipat+1);
            return MatchPattern(str.Substring(str.Length-pattern.Length), pattern, wc, wsc, senseCase);
          }
          var sc = str[istr];
          if (!charEqual(sc, pc, senseCase)) return false;
       }
       return str.Length==pattern.Length;
    }
public static bool MatchPattern ( this string str , string pattern , char wc = ' ' , char wsc = ' ' , bool senseCase = false ) { if ( str . IsNullOrWhiteSpace ( ) && pattern . IsNullOrWhiteSpace ( ) ) return true ; if ( pattern . IsNullOrWhiteSpace ( ) ) return false ; var iwc = pattern . IndexOf ( wc ) ; if ( iwc >= 0 ) { var iwc2 = pattern . LastIndexOf ( wc ) ; if ( iwc2 > iwc ) throw new NFXException ( StringConsts . TEXT_PATTERN_MULTI_WC_ERROR . Args ( pattern , wc ) ) ; } int istr = 0 , ipat = 0 ; for ( ; istr < str . Length && ipat < pattern . Length ; istr ++ , ipat ++ ) { var pc = pattern [ ipat ] ; if ( pc == wsc ) continue ; if ( pc == wc ) { var leftcnt = pattern . Length - 1 - ipat ; if ( leftcnt <= 0 ) return true ; if ( istr >= str . Length - 1 ) return false ; pattern = pattern . Substring ( ipat + 1 ) ; return MatchPattern ( str . Substring ( str . Length - pattern . Length ) , pattern , wc , wsc , senseCase ) ; } var sc = str [ istr ] ; if ( ! charEqual ( sc , pc , senseCase ) ) return false ; } return str . Length == pattern . Length ; }
Returns true if supplied string matches pattern that can contain up to one * wildcard and multiple ?
Returns true if supplied string matches pattern that can contain up to one * wildcard and multiple ?
//     some address //     some*e?s //? //* //match whatever left * at the end //nothing left in string but something left in pattern
string
this
pattern
string
wc
char
wsc
char
senseCase
bool