You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nextflow中如何使用正则表达式过滤Path类型通道?

问题

针对字符串类型的通道,以下代码可以正常运行:

Channel
.of('test1.fastq', 'test2.fastq', 'test1.bam', 'test2.bam')
.filter( ~/.*bam|.*1.fastq/ )
.view()

但同样的正则过滤方式对Path类型的通道无效,比如下面的代码无法正常工作:

Channel
.fromPath( params.inputs )
.filter( ~/.*bam|.*1.fastq/ )
.view()

请问有没有方法让正则过滤适用于Path类型的通道?

解决方案

fromPath生成的通道元素是Path对象,不是字符串,直接用正则匹配会失败,你可以用以下两种方法解决:

方法1:匹配文件名

提取Path对象的文件名(忽略路径前缀)进行正则匹配,适合只需要筛选文件名的场景:

Channel
.fromPath( params.inputs )
.filter { path -> path.getFileName().toString() ==~ /.*bam|.*1.fastq/ }
.view()

方法2:匹配完整路径

将Path对象转为完整路径字符串后再匹配,适合需要基于完整路径筛选的场景:

Channel
.fromPath( params.inputs )
.filter { path -> path.toString() ==~ /.*bam|.*1.fastq/ }
.view()

内容的提问来源于stack exchange,提问作者aerijman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 11:07:04