Python wildcard search in string

Question:

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’

Asked By: Austin

||

Answers:

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.

Answered By: Kos

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.

Answered By: phihag

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)]
Answered By: Yuushi

You could try the fnmatch module, it’s got a shell-like wildcard syntax

or can use regular expressions

import re

Answered By: Munai Das Udasin

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)
Answered By: Farhaan S.

Easy method is try os.system:

import os
text = 'this is text'
os.system("echo %s | grep 't*'" % text)
Answered By: Harry1992

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)
Answered By: Michel Soares
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.