如何在 Python 中使用通配符创建搜索词?
我想检查文档中是否包含某个术语。然而,有时,这个词有多种形式(复数、过去时等)。
'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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用正则表达式并循环遍历文件:
Use regular expressions and just loop through the file:
我通常会选择正则表达式,但如果由于某种原因您想坚持使用通配符格式,您可以这样做:
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:
您描述的 * 语法称为 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.
如果您要做任何复杂的事情,正则表达式是最佳选择。如果您对这些不满意,我认为对于您的具体问题,您也可以使用“in”。例如:
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:
你可以使用正则表达式吗?
更多信息:
http://docs.python.org/library/re.html
can you use a regular expression?
more here:
http://docs.python.org/library/re.html