[英]python - find all occurrences of substring with wildcards in string
I am trying to write a function to return all occurrences of a substring that contains wildcards (each wildcard accounting for only one character) within a longer string.我正在尝试编写一个函数来返回在较长字符串中包含通配符(每个通配符仅占一个字符)的子字符串的所有出现。 For instance, let's say I have the subject string: aabcddcabaabedcbabaa
and my query string is b?d??ab
.例如,假设我有主题字符串: aabcddcabaabedcbabaa
并且我的查询字符串是b?d??ab
。 The expected output would be: ['bcddcab', 'bedcbab']
预期输出为: ['bcddcab', 'bedcbab']
Looking through other stack overflow posts, I've tried the following:查看其他堆栈溢出帖子,我尝试了以下操作:
import fnmatch
subject = "aabcddcabaabedcbabaa"
query = "b?d??ab"
res = fnmatch.filter(subject, query)
but this returns an empty list.但这会返回一个空列表。 What am I doing wrong?我究竟做错了什么? Am I actually using the filter
function of fnmatch
correctly?我真的正确使用了fnmatch
的filter
功能吗? Thank you in advance先感谢您
filter
, not the first查询应该是filter
的第二个参数,而不是第一个filter
filters a list of strings by keeping the strings that match your query. filter
通过保留与您的查询匹配的字符串来过滤字符串列表。 filter
does not return a list of substrings of a string. filter
不会返回一个字符串的子串的列表。 If you want to filter the substrings with filter
, you first need to build the list of substrings:如果要筛选与子filter
,你首先需要建立子列表:import fnmatch
subject = "aabcddcabaabedcbabaa"
query = "b?d??ab"
substrings = fnmatch.filter((subject[i:i+len(query)] for i in range(len(subject) - len(query))), query)
print(substrings)
Output: ['bcddcab', 'bedcbab']
输出: ['bcddcab', 'bedcbab']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.