Reputation: 4566
Lets say that I have a list
list = ['this','is','just','a','test']
how can I have a user do a wildcard search?
Search Word: 'th_s'
Would return 'this'
Upvotes: 114
Views: 445025
Reputation: 1
Why don't you just use the join function? In a regex findall() or group() you will need a string so:
import re
regex = re.compile('th.s')
l = ['this', 'is', 'just', 'a', 'test']
matches = re.findall(regex, ' '.join(l)) #Syntax option 1
matches = regex.findall(' '.join(l)) #Syntax option 2
The join() function allows you to transform a list in a string. The single quote before join is what you will put in the middle of each string on list. When you execute this code part (' '.join(l)) you'll receive this:
'this is just a test'
So you can use the findal() function.
I know i am 7 years late, but i recently create an account because I'm studying and other people could have the same question. I hope this help you and others.
Update After @FélixBrunet comments:
import re
regex = re.compile(r'th.s')
l = ['this', 'is', 'just', 'a', 'test','th','s', 'this is']
matches2=[] #declare a list
for i in range(len(l)): #loop with the iterations = list l lenght. This avoid the first item commented by @Felix
if regex.findall(l[i]) != []: #if the position i is not an empty list do the next line. PS: remember regex.findall() command return a list.
if l[i]== ''.join(regex.findall(l[i])): # If the string of i position of l list = command findall() i position so it'll allow the program do the next line - this avoid the second item commented by @Félix
matches2.append(''.join(regex.findall(l[i]))) #adds in the list just the string in the matches2 list
print(matches2)
Upvotes: 0
Reputation: 104
Same idea as Yuushi in using regular expressions, but this uses the findall method within the re library instead of a list comprehension:
import re
regex = re.compile('th.s')
l = ['this', 'is', 'just', 'a', 'test']
matches = re.findall(regex, string)
Upvotes: 0
Reputation: 287825
Use fnmatch
:
import fnmatch
lst = ['this','is','just','a','test']
filtered = fnmatch.filter(lst, 'th?s')
If you want to allow _
as a wildcard, just replace all underscores with '?'
(for one character) or *
(for multiple characters).
If you want your users to use even more powerful filtering options, consider allowing them to use regular expressions.
Upvotes: 214
Reputation: 26040
Regular expressions are probably the easiest solution to this problem:
import re
regex = re.compile('th.s')
l = ['this', 'is', 'just', 'a', 'test']
matches = [string for string in l if re.match(regex, string)]
Upvotes: 65
Reputation: 72241
Do you mean any specific syntax for wildcards? Usually *
stands for "one or many" characters and ?
stands for one.
The simplest way probably is to translate a wildcard expression into a regular expression, then use that for filtering the results.
Upvotes: 0