🌐
Reddit
reddit.com › r/learnpython › how to search strings in python using a wildcard that represents only a single word -- and not multiple words as well?
r/learnpython on Reddit: How to search strings in Python using a wildcard that represents only a single word -- and not multiple words as well?
January 26, 2023 -

fnmatch is pretty simple in Python -- however it will output "True" whether there is 1, or 100 words, between the words you've put the wildcard between.

I'd like to be more narrow than this -- and be able to use some kind of wildcard searching library that let's me specify HOW MANY words I want to be wildcards.

So if I used: "the * cat", it would ONLY include single words like "the ugly cat" or "the furry cat"

But if I used something like: "the ** cat", it would include ONLY two words like "the very ugly cat" or "the extremely furry cat"

Is there any python library that allows this kind of fine-tuned wildcard functionality?

Thanks!

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-wildcard-substring-search
Python - Wildcard Substring search - GeeksforGeeks
April 9, 2023 - Use a list comprehension to iterate through the input string and generate a list of substrings that match the given wildcard pattern. In this case, we use string slicing and endswith() method for comparison.
🌐
TutorialsPoint
tutorialspoint.com › wildcard-matching-in-python
Wildcard Matching in Python
Suppose we have an input string s and another input string p. Here is the main string and p is the pattern. We have to define one method, that can match pattern in the string. So we have to implement this for a regular expression, that supports wildcard characters like ‘?’ And ‘*’.
🌐
Python
docs.python.org › 3 › library › fnmatch.html
fnmatch — Unix filename pattern matching
The special characters used in shell-style wildcards are: For a literal match, wrap the meta-characters in brackets. For example, '[?]' matches the character '?'. Note that the filename separator ('/' on Unix) is not special to this module.
🌐
LeetCode
leetcode.com › problems › wildcard-matching
Wildcard Matching - LeetCode
Wildcard Matching - Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*' where: * '?' Matches any single character. * '*' Matches any sequence of characters (including the empty sequence).
🌐
Educative
educative.io › answers › how-to-implement-wildcards-in-python
How to implement wildcards in Python
In Python, we can implement wildcards using the regex (regular expressions) library. The dot . character is used in place of the question mark ... ?? symbol. Hence,​ to search for all words matching the col?r pattern, the code would look something like as follows.
Top answer
1 of 2
10

Regex, like the accepted answer suggests, is one way of handling the problem. Although, if you need a simpler pattern (such as Unix shell-style wildcards), then the fnmatch built in library can help:

Expressions:

  • * - matches everything
  • ? - matches any single character
  • [seq] - matches any character in seq
  • [!seq] - matches any character not in seq

So for example, trying to find anything that would match with localhost:

import fnmatch

my_pattern = "http://localhost*"
name_to_check = "http://localhost:8080"

fnmatch.fnmatch(name_to_check, my_pattern) # True

The nice part of this is that / is not considered a special character, so for filename/URL matching this works out quite well without having to pre-escape all slashes!

2 of 2
1

It looks like you're essentially implementing a subset of regular expressions. Luckily, Python has a library for that built-in! If you're not familiar with how regular expressions (or, as their friends call them, regexes) work, I highly recommend you read through the documentation for them.

In any event, the function re.search is, I think, exactly what you're looking for. It takes, as its first argument, a pattern to match, and, as its second argument, the string to match it in. If the pattern is matched, search returns an SRE_Match object, which, conveniently, has a #start() method that returns the index at which the match starts.

To use the data from your example:

 import re
 start_index = re.search(r'x.z', 'xxxxxgzg').start()

Note that, in regexes, . - not * -- is the wildcard, so you'll have to replace them in the pattern you're using.

Find elsewhere
🌐
Wing Python IDE
wingware.com › doc › edit › search-wildcard
Wildcard Search Syntax - Wing Python IDE
* matches any sequence of characters except for line endings. For example, the search string my*value would match anything within a single line of text starting with my and ending with value.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › wildcard-pattern-matching
Wildcard Pattern Matching - GeeksforGeeks
November 10, 2025 - DSA Python · Last Updated : 10 Nov, 2025 · Given two strings pat and txt which may be of different lengths. Check if the wildcard pattern(pat) matches with txt or not. The wildcard pattern pat can include the characters '?' and '*'. '?' – ...
🌐
Delft Stack
delftstack.com › home › howto › python › python wildcard
What Is the Python Wildcard | Delft Stack
March 11, 2025 - Wildcards are invaluable tools in Python, enabling developers to perform efficient pattern matching and searching. Whether you choose to use the fnmatch or glob modules for file operations, or the re module for more complex string matching, understanding how to implement wildcards will undoubtedly enhance your programming skills.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-filter-list-of-strings-using-wildcard
Filter a List of Strings using a Wildcard in Python | bobbyhadz
If you want to check if a string matches a pattern using a wildcard, use the fnmatch.fnmatch() method.
🌐
Vectorworks Community Board
forum.vectorworks.net › customization › python scripting
if condition with wildcards in strings in python? - Python Scripting - Vectorworks Community Board
March 25, 2021 - just a simple question: I want to scan object styles. for example "BA-I-BST-370-230-BFT" contains the letters *BST* in a worksheet I would write sth like =if('stylename'='*BST'; yes;no) in Python I tried (running in a foreach-procedure, it has to filter all slab styles, which names contain the le...
🌐
Finxter
blog.finxter.com › 5-best-ways-to-implement-wildcard-matching-in-python
5 Best Ways to Implement Wildcard Matching in Python – Be on the Right Side of Change
This snippet uses the re module to compile a regex pattern corresponding to the wildcard pattern “*.txt“. Then it checks if the pattern matches a given string, outputting True if there’s a match. The dollar sign ensures the string ends with “.txt”. For file directory wildcard matching, Python’s pathlib module is highly effective.
🌐
Javatpoint
javatpoint.com › wildcards-in-python
Wildcards in Python - Javatpoint
Python Program to Count the Number of Matching Characters in a Pair of String · Ping Pong Game Using Turtle in Python · Python Function to Display Calendar · Python Program for Calculating the Sum of Squares of First n Natural Numbers · Python Program for How to Check if a Given Number is Fibonacci Number or Not ·
🌐
PREP INSTA
prepinsta.com › home › python program › python program to check if two strings match where one string contains wildcard characters
One String Contain Wild card character - Python Program
October 14, 2022 - If they match, continue checking the next characters. Else, return false. Print result. ... def solve(a,b): n,m=len(a),len(b) if n==0 and m==0: return True if n > 1 and a[0] == '*' and m == 0: return False if (n > 1 and a[0] == '?') or (n != 0 and m !=0 and a[0] == b[0]): return solve(a[1:],b[1:]); if n !=0 and a[0] == '*': return solve(a[1:],b) or solve(a,b[1:]) return False str1="Prepins*a" str2="Prepinsta" print("First string with wild characters :" , str1) print("Second string without wild characters ::" , str2) print(solve(str1,str2))
🌐
Finxter
blog.finxter.com › home › learn python blog › python filter list of strings using a wildcard
Python Filter List of Strings Using a Wildcard - Be on the Right Side of Change
February 4, 2024 - 💡Problem Formulation: In Python, a common task is to filter a list of strings based on some pattern which may include wildcards (also called asterisks or star * operators). A wildcard character can represent one or multiple characters, making ...
🌐
Python
docs.python.org › 3.4 › howto › regex.html
Regular Expression HOWTO — Python 3.4.10 documentation
Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the re module. Using this little language, you specify the rules for the set of possible strings that you want to match; ...
🌐
GitHub
github.com › enggen › LeetCode › blob › master › Python › wildcard-matching.py
LeetCode/Python/wildcard-matching.py at master · enggen/LeetCode
# Implement wildcard pattern matching with support for '?' and '*'. # # '?' Matches any single character. # '*' Matches any sequence of characters (including the empty sequence). # # The matching should cover the entire input string (not partial). # # The function prototype should be: # bool isMatch(const char *s, const char *p) # # Some examples: # isMatch("aa","a") -> false ·
Author   enggen