[英]How can I support wildcards in user-defined search strings in Python?
Is there a simple way to support wildcards ("*") when searching strings - without using RegEx? 在搜索字符串时是否有一种简单的方法来支持通配符(“*”) - 而不使用RegEx?
Users are supposed to enter search terms using wildcards, but should not have to deal with the complexity of RegEx: 用户应该使用通配符输入搜索条件,但不应该处理RegEx的复杂性:
"foo*" => str.startswith("foo")
"*foo" => str.endswith("foo")
"*foo*" => "foo" in str
(it gets more complicated when there are multiple search terms though, eg "foo bar baz") (当有多个搜索词时,它会变得更复杂,例如“foo bar baz”)
This seems like a common issue, so I wonder whether there's a ready-made solution for it. 这似乎是一个常见的问题,所以我想知道是否有一个现成的解决方案。
Any help would be greatly appreciated! 任何帮助将不胜感激!
你可以试试fnmatch
模块,它有一个类似shell的通配符语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.