How can I create search terms with wildcards in Python?
Solution 1
Use regular expressions and just loop through the file:
import re
f=open('test.file.here', 'r')
pattern = re.compile("^[^\s]*ello[^\s]*\sWorld[^\s]*$")
for line in f:
if pattern.match(line):
print line,
f.close()
Solution 2
I would usually opt for a regular expression, but if for some reason you want to stick to the wildcard format, you can do this:
from fnmatch import fnmatch
pattern = '*ello* World*'
with open('sample.txt') as file:
for line in f:
if fnmatch(line, pattern):
print(line)
Solution 3
If you're doing anything complicated, regular expressions are the way to go. If you're not comfortable with those, I think for your specific question you could also use "in". For example:
x = 'hello world'
if 'ello' in x and 'world' in x':
print 'matches'
else:
print 'does not match'
Solution 4
The * syntax you describe is known as globbing. It doesn't work for documents, just files and directories. Regular expressions, as others have noted, are the answer.
Solution 5
can you use a regular expression?
import re
m = re.search('\.*ello', somefile)
more here:
coderman
Updated on July 29, 2022Comments
-
coderman almost 2 years
I want to check whether a certain term is contained in a document. However, sometimes, the word is in several forms (plural, past tense, etc).
'Hello Worlds' 'Hellos Worlds' 'Jello World' 'Hello Worlded'
How can I create a search term which will find all instances such as
'*ello* World*'
where star is a wild card that doesn't necessarily have to be included in the word.
I found documentation for an fnmatch module, but I can't see how that can help me search through a document.
-
Ignacio Vazquez-Abrams about 13 yearsExcept for the existence of
fnmatch
. -
coderman about 13 yearsThanks so much @photoionized. This is exactly what I was looking for.
-
Brian O'Dell about 13 yearsaccording to the docs, fnmatch is meant for use on filenames.
-
Ignacio Vazquez-Abrams about 13 yearsIt uses "filename" as an argument an awful lot, but nowhere does it say that it's only for use on filenames.
-
Brian O'Dell about 13 yearsAfter a little playing around, it seems it will work if you begin and end your pattern with a *
-
Hans Goldman over 4 years"Regular expressions are usually better." Yeah, well... that's just like your opinion, man. haha I would say that they are better when you're searching for something very precise where wildcards can't work. For everything else, wildcards are probably better. Easier to learn, easier to write, and easier to maintain. But, hey... That's just like my opinion, man... :)
-
seddonym over 4 yearsYou make a good point! I've edited the post to tone down my opinion.