如何将正则表达式提供给需要字符串的方法?

时间:2015-11-12 16:53:37

标签: python regex h5py

我想要达到的目标是基于前一个问题的投票答案:Check if node exists in h5py

基本上我想替换:

"/some/path" in h5File

有类似的东西:

import re
re.compile(r'/some/[pattern]+') in h5File

1 个答案:

答案 0 :(得分:2)

in关键字无法使用正则表达式,但您可以使用list-comprehension或filter内置来从字典中获取与给定正则表达式匹配的所有键。

E.g。

found_keys = [k for k in h5file if re.match(r'/some/[pattern]+', k)]

regex = re.compile(r'/some/[pattern]+')
found_keys = filter(regex.match, h5file)
相关问题