How can I create search terms with wildcards in Python?

13,855

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:

http://docs.python.org/library/re.html

Share:
13,855
coderman
Author by

coderman

Updated on July 29, 2022

Comments

  • coderman
    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
    Ignacio Vazquez-Abrams about 13 years
    Except for the existence of fnmatch.
  • coderman
    coderman about 13 years
    Thanks so much @photoionized. This is exactly what I was looking for.
  • Brian O'Dell
    Brian O'Dell about 13 years
    according to the docs, fnmatch is meant for use on filenames.
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams about 13 years
    It uses "filename" as an argument an awful lot, but nowhere does it say that it's only for use on filenames.
  • Brian O'Dell
    Brian O'Dell about 13 years
    After a little playing around, it seems it will work if you begin and end your pattern with a *
  • Hans Goldman
    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
    seddonym over 4 years
    You make a good point! I've edited the post to tone down my opinion.