Tamas Koos
Tamas Koos

Reputation: 1063

Python - search list with only a few characters entered

I want to find the string that the user searches for. For example, if the user enters FD-2** (he doesnt know what the * characters are), the search should give FD-234 and FD-285. I have a piece of code:

rendszam = input('Adja meg a rendszámot! ')
matching = [s for s in rszamok if rendszam in s]
print(matching)

How can I do that?

Upvotes: 0

Views: 55

Answers (1)

Raymond Hettinger
Raymond Hettinger

Reputation: 226366

The easiest way is to use a regular expression:

>>> import re
>>> targets = 'FD-234 XY-456 FD-285 XY-890 FD-999'
>>> search = 'FD-2**'
>>> pattern = search.replace('*', '.')
>>> re.findall(pattern, targets)
['FD-234', 'FD-285']

Upvotes: 3

Related Questions