Perl 6 的 comb 方法的 Python 等价物是什么?

What is the Python equivalent of Perl 6's comb method?

comb 在 Perl 6 中就像 split 的补充。您不是选择从结果中 排除 的内容,而是选择 包含 的内容。是否有 Python 等价物,如果有,它是什么?

到目前为止,我对 "Python comb" 的所有搜索都为我提供了与 "combinations" 相关的结果,而不是 "the complement of split"。

这是 Perl 6 中的一个例子:

#!/bin/env perl6

my $text = "5 foos, 16 bars, 7 bazes";

my @result = $text.comb(/\d+/);  # \d matches numbers

say @result.join(" ");  # 5 16 7

根据反馈更新:comb 比 "opposite" 更像是 split 的补充。

您可以将 str.joinre.findall

一起使用

例如:

import re
text = "5 foos, 16 bars, 7 bazes"
print(" ".join(re.findall(r"\d+", text)))

输出:

5 16 7