Matching wildcards explained

In computer science, an algorithm for matching wildcards (also known as globbing) is useful in comparing text strings that may contain wildcard syntax.[1] Common uses of these algorithms include command-line interfaces, e.g. the Bourne shell[2] or Microsoft Windows command-line[3] or text editor or file manager, as well as the interfaces for some search engines[4] and databases.[5] Wildcard matching is a subset of the problem of matching regular expressions and string matching in general.[6]

The problem

A wildcard matcher tests a wildcard pattern p against an input string s. It performs an anchored match, returns true only when p matches the entirety of s.

The pattern can be based on any common syntax (see globbing), but on Windows programmers tend to only discuss a simplified syntax supported by the native C runtime:[7]

This article mainly discusses the Windows formulation of the problem, unless otherwise stated.

Definition

Stated in zero-based indices, the wildcard-matching problem can be defined recursively as:

\begin{aligned} m00&=(p0=t0)\\ m0j&=(pj-1=‘*’)\landm0,j-1\\ mi0&=false\\ mij&= \begin{cases} mi-1,&forpi-1=tj-1\lorpi-1=‘?’\\ mi,\lormi-1,&forpi-1=‘*’\\ false&forpi-1tj-1\end{cases}&&for1\leqi\le|p|,1\leqj\le|t|. \end{aligned}

where mij is the result of matching the pattern p against the text t truncated at i and j characters respectively. This is the formulation used by Richter's algorithm and the Snippets algorithm found in Cantatore's collection. This description is similar to the Levenshtein distance.

Related problems

Directly related problems in computer science include:

History

Early algorithms for matching wildcards often relied on recursion, but the technique was criticized on grounds of performance and reliability[11] considerations. Non-recursive algorithms for matching wildcards have gained favor in light of these considerations.

Among both recursive and non-recursive algorithms, strategies for performing the pattern matching operation vary widely, as evidenced among the variety of example algorithms referenced below. Test case development and performance optimization techniques have been demonstrably brought to bear on certain algorithms, particularly those developed by critics of the recursive algorithms.

Recursive algorithms

The recursion generally happens on matching * when there is more suffix to match against. This is a form of backtracking, also done by some regular expression matchers.

The general form of these algorithms are the same. On recursion the algorithm slices the input into substrings, and considers a match to have happened when ONE of the substrings return a positive match. For, it would greedily call,, and . They usually differ by less-important things like support for features and by more important factors such as minor but highly effective optimizations. Some of them include:

Martin Richter's algorithm is an exception to this pattern, although the overall operation is equivalent. On * it recurses into increasing either of the indexes, following the dynamic programming formulation of the problem. The "ABORT" technique is applicable to it as well.[15] On typical patterns (as tested by Cantatore) it is slower than the greedy-call implementations.

The recursive algorithms are in general easier to reason about, and with the ABORT modification they perform acceptably in terms of worst-case complexity. On strings without * they take linear-to-string-size time to match since there is a fixed one-to-one relation.

Non-recursive algorithms

The following are developed by critics of the recursive algorithms:

The following is not:

The iterative functions above implement backtracking by saving an old set of pattern/text pointers, and reverting to it should a match fails. According to Kurt, since only one successful match is required, only one such set needs to be saved.[23]

In addition, the problem of wildcard matching can be converted into regular expression matching using a naive text-replacement approach. Although non-recursive regular expression matchers such as Thompson's construction are less used in practice due to lack of backreference support, wildcard matching in general does not come with a similarly rich set of features. (In fact, many of the algorithms above only has support for and .) The Russ Cox implementation of Thompson NFA can be trivially modified for such.[26] Gustavo Navarro's -based nrgrep algorithm provides a more streamlined implementation with emphasis on efficient suffixes.[27] See also .

See also

Notes and References

  1. Web site: Wildcard characters. ScienceDirect. 2018.
  2. Book: Quigley, Ellie. UNIX Shell Programming QuickStart. InformIT.com. 2005.
  3. Web site: MS-DOS and Windows Wildcard Characters. Microsoft Developer Network Library.
  4. Web site: Apache Lucene - Query Parser Syntax. Apache Lucene 2.9.4 Documentation. 2006.
  5. Web site: SQL Wildcards. W3Schools. 2018.
  6. Web site: Goyvaerts. Jan. Welcome to Regular-Expressions.info. RegularExpressions.info. 2018.
  7. Web site: Wildcard Expansion . docs.microsoft.com . en-us.
  8. Iliopoulos . Costas S. . Rahman . M. Sohel . Pattern Matching Algorithms with Don't Cares . SOFSEM 2007: Theory and Practice of Computer Science, 33rd Conference on Current Trends in Theory and Practice of Computer Science . 2007 . https://web.archive.org/web/20191217184544/https://pdfs.semanticscholar.org/fd93/e240369270e3f2958de515dcb42a53647de2.pdf . dead . 2019-12-17 . Harrachov, Czech Republic. 14538871 .
  9. Clifford . Peter . Clifford . Raphaël . Simple deterministic wildcard matching . Information Processing Letters . January 2007 . 101 . 2 . 53–54 . 10.1016/j.ipl.2006.08.002.
  10. Wu . Xindong . Qiang . Ji-Peng . Xie . Fei . Pattern Matching with Flexible Wildcards . Journal of Computer Science and Technology . 12 September 2014 . 29 . 5 . 740–750 . 10.1007/s11390-014-1464-3. 16824910 .
  11. Krauss. Kirk. Matching Wildcards: An Algorithm. Dr. Dobb's Journal. 2008.
  12. Web site: Salz. Rich. wildmat.c. GitHub. 1991.
  13. Web site: Filip. Compare strings with wildcard. Stack Overflow. 2014.
  14. Web site: Murugesan. Vignesh. 2014. WildCard Matching algorithm.
  15. Web site: Deadlock. Wildcard Matching Recursive Algorithm C++. Stack Overflow. 2015.
  16. Web site: van Rossum . Guido . freebsd/lib/libc/gen/fnmatch.c . . 21 November 2019 . 20 November 2019.
  17. Web site: fnmatch.c. opensource.apple.com. 1999.
  18. Web site: fnmatch_internal.c . Beren Minor's Mirrors . 21 November 2019.
  19. Web site: git/git: wildmatch.c . GitHub. 2020-01-20 .
  20. Web site: uwildmat.c in trunk/lib – INN . inn.eyrie.org . 27 November 2019.
  21. Web site: Krauss. Kirk. Matching Wildcards: An Improved Algorithm for Big Data. Develop for Performance. 2018.
  22. Web site: Cantatore. Alessandro. Wildcard matching algorithms. 2003.
  23. Web site: Kurt . Dogan . Wildcard Matching Methods .
  24. Web site: Siler. Recursive solutions for glob pattern matching. Stack Overflow. 2013.
  25. Web site: Handy. Jack. Wildcard string compare (globbing). Code Project. 2005.
  26. Web site: Cox . Ross . Regular Expression Matching Can Be Simple And Fast .
  27. Navarro . Gonzalo . NR-grep: a fast and flexible pattern-matching tool . Software: Practice and Experience . 10 November 2001 . 31 . 13 . 1265–1312 . 10.1002/spe.411 . 3175806 .