如何在 Python 中使用通配符创建搜索词?

发布于 2024-11-04 08:01:19 字数 276 浏览 1 评论 0原文

我想检查文档中是否包含某个术语。然而,有时,这个词有多种形式(复数、过去时等)。

'Hello Worlds'
'Hellos Worlds'
'Jello World'
'Hello Worlded'

如何创建一个搜索词来查找所有实例,例如

'*ello* World*'

星号是通配符,不一定必须包含在单词中。

我找到了 fnmatch 模块的文档,但我看不出它如何帮助我搜索文档。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

救星 2024-11-11 08:01:19

使用正则表达式并循环遍历文件:

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()

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()
柳絮泡泡 2024-11-11 08:01:19

我通常会选择正则表达式,但如果由于某种原因您想坚持使用通配符格式,您可以这样做:

from fnmatch import fnmatch

pattern = '*ello* World*'

with open('sample.txt') as file:
    for line in f:
        if fnmatch(line, pattern):
            print(line)

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)
舂唻埖巳落 2024-11-11 08:01:19

您描述的 * 语法称为 globbing。它不适用于文档,仅适用于文件和目录。正如其他人所指出的,正则表达式就是答案。

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.

清风疏影 2024-11-11 08:01:19

如果您要做任何复杂的事情,正则表达式是最佳选择。如果您对这些不满意,我认为对于您的具体问题,您也可以使用“in”。例如:

x = 'hello world'
if 'ello' in x and 'world' in x':
     print 'matches'
else:
     print 'does not match'

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'
温馨耳语 2024-11-11 08:01:19

你可以使用正则表达式吗?

import re
m = re.search('\.*ello', somefile)

更多信息:

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

can you use a regular expression?

import re
m = re.search('\.*ello', somefile)

more here:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文