What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Wildcard Pattern Matching (with Examples & Code)

  • Sep 17, 2024
  • 6 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Vedanti Kshirsagar
Wildcard Pattern Matching (with Examples & Code)

Wildcard pattern matching is a valuable technique used in computer science and software development to compare patterns containing wildcard characters with target strings. With its ability to represent flexible matching criteria, wildcard pattern matching provides a powerful tool for various applications. This article explores the concept of wildcard pattern matching, its significance, commonly used algorithms, and practical use cases.

Understanding Wildcard Pattern Matching

Wildcard pattern matching involves comparing a pattern that contains wildcard characters, such as asterisks (*) or question marks (?), with a target string. The wildcard characters act as placeholders, allowing for flexible matching of different characters or sets of characters. Wildcard pattern matching enables developers to search, filter, or manipulate strings based on broad matching rules.

Common Wildcard Characters:

  1. Asterisk (*) - Matches zero or more characters.
  2. Question Mark (?) - Matches a single character.

Example 1

Pattern: "c*t"

Target Strings: "cat", "cot", "coat", "cabaret"

In this example, the pattern "ct" uses the wildcard character '' to match any sequence of characters between 'c' and 't'. The target strings "cat", "cot", "coat", and "cabaret" all match the pattern because they have a 'c' followed by any number of characters and ending with 't'.

Example 2

Pattern: "*ing"

Target Strings: "running", "jumping", "singing", "swing"

In this example, the pattern "ing" uses the wildcard character '' at the beginning to match any sequence of characters followed by "ing". The target strings "running", "jumping", and "singing" match the pattern because they start with any characters and end with "ing". However, the target string "swing" does not match the pattern because it does not end with "ing".

Example 3

Pattern: "te?t"

Target Strings: "test", "text", "tent", "thought"

In this example, the pattern "te?t" uses the wildcard character '?' to match any single character. The target strings "test" and "text" match the pattern because they have 'te', followed by any character, and end with 't'. The target strings "tent" and "thought" do not match the pattern because they either have more than one character or do not have 't' as the last character.

Example 4

Pattern: "a*b?c"

Target Strings: "alphabet", "abacus", "arc"

In this example, the pattern "ab?c" uses both the wildcard characters '' and '?'. The target string "alphabet" matches the pattern because it starts with 'a', followed by any number of characters, then 'b', followed by a single character, and ending with 'c'. The target string "abacus" also matches the pattern, but the target string "arc" does not match because it does not have 'b' followed by a single character.

These examples demonstrate how wildcard pattern matching can be used to compare patterns with target strings, allowing for flexible and powerful matching capabilities.

Algorithm & Code

One of the commonly used algorithms for wildcard pattern matching is the recursive backtracking algorithm. This algorithm systematically compares the pattern with the target string, considering wildcard characters and exploring all possible matches. Here is a step-by-step outline of the recursive backtracking algorithm for wildcard pattern matching:

  1. Start with the pattern and the target string.
  2. If the pattern and the target string are both empty, return true (indicating a match).
  3. If the pattern is empty but the target string is not, return false (indicating no match).
  4. If the target string is empty: a. If the pattern starts with '', recursively call the algorithm by removing the '' and the first character from the pattern. b. Otherwise, return false (indicating no match).
  5. Compare the first characters of the pattern and the target string. a. If the characters are equal or the pattern character is '?', recursively call the algorithm by removing the first character from both the pattern and the target string. b. If the pattern character is '', recursively call the algorithm in two ways: i. By removing the '' and the first character from the pattern. ii. By removing the first character from the target string. c. If the characters are not equal and the pattern character is not '*', return false (indicating no match).
  6. If any of the recursive calls in step 5 return true, return true (indicating a match).
  7. If none of the recursive calls in step 5 return true, return false (indicating no match).

Here is the code to implement it:

#include 
using namespace std;

bool isMatch(const string& pattern, const string& target) {
    if (pattern.empty() && target.empty())
        return true;
    if (pattern.empty())
        return false;
    if (target.empty()) {
        if (pattern[0] == '*')
            return isMatch(pattern.substr(1), target);
        return false;
    }

    if (pattern[0] == target[0] || pattern[0] == '?')
        return isMatch(pattern.substr(1), target.substr(1));
    if (pattern[0] == '*')
        return isMatch(pattern.substr(1), target) || isMatch(pattern, target.substr(1));

    return false;
}

int main() {
    string pattern = "wild*ing";
    string target = "wildcard matching";

    if (isMatch(pattern, target))
        cout << "Match found!" << endl;
    else
        cout << "No match found." << endl;

    return 0;
}

 

Output:

Match found!

 

In this code, the isMatch function takes two string parameters: the pattern and the target string. It recursively compares the characters of the pattern and target string, considering the wildcard characters '*' and '?'. The substr function is used to extract substrings from the pattern and target string during the recursive calls.

The main function demonstrates an example usage of the isMatch function by checking if the pattern "wild*ing" matches the target string "wildcard matching". It prints the appropriate message based on whether a match is found or not.

Time & Space Complexity

In the worst case, where the pattern and target string have lengths n and m, respectively, the time complexity of the recursive backtracking algorithm is O(2^(n+m)). This is because the algorithm explores all possible combinations of matches, including the recursive calls with and without wildcard expansion.

As a result, the number of recursive calls grows exponentially with the combined length of the pattern and target string.

The space complexity of the code is determined by the recursive calls and the substring operations. In each recursive call, memory is allocated for the new substrings created using the substr function. In the worst case, the depth of the recursive calls can reach the length of the pattern or the target string, resulting in a space complexity of O(n+m).

Applications of Wildcard Pattern Matching

Wildcard pattern matching finds wide-ranging applications across different domains and industries:

  • File Systems: Wildcard pattern matching is extensively used in file systems for searching and filtering files based on patterns. For example, the command "ls *.txt" in a Unix-like system lists all files with the ".txt" extension.

  • Text Processing: In text processing tasks, wildcard pattern matching allows for efficient search and manipulation operations. Text editors, search engines, and data extraction tools utilize wildcard patterns to find and extract specific information from large text datasets.

  • Data Validation: Wildcard pattern matching is employed for data validation, ensuring that user input matches specific patterns or formats. For instance, email validation can use wildcard patterns to check if an email address follows the correct format.

  • Routing and URL Matching: In web development and networking, wildcard pattern matching assists in routing and URL matching. It enables developers to define flexible URL patterns, facilitating dynamic routing and handling of different URLs.

  • Test Automation: Wildcard pattern matching is valuable in test automation scenarios, allowing for dynamic matching of test cases or expected outputs. Test frameworks can utilize wildcard patterns to validate test results against expected outcomes.

Conclusion

Wildcard pattern matching provides a versatile and powerful technique for comparing patterns with target strings. By incorporating wildcard characters, developers can create flexible matching criteria that cater to a wide range of applications. Understanding the concept of wildcard pattern matching, its time complexity, and practical use cases empowers programmers to effectively analyze, search, and manipulate strings in diverse software development scenarios.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Vedanti Kshirsagar
I am a fervid coder on LeetCode and have participated in various hackathons. I enjoy coding and learning new things in this field every day. I am a self-motivated, curious, and disciplined individual who looks forward to sharing and expanding my knowledge in any way possible