gpt4 book ai didi

python - 使用 Python 的正则表达式捕获字符串的子集

转载 作者:太空宇宙 更新时间:2023-11-04 09:02:02 25 4
gpt4 key购买 nike

我有一个看起来像这样的字符串:

>Bounded_RNA_of:1DDL:Elength : 1

正则表达式可以这样形成:

>Bounded_RNA_of:(\w+):(\w)length : 1

归根结底,我想要提取的只是 1DDLE

但是为什么这个正则表达式失败了?

import re
seq=">Bounded_RNA_of:1DDL:Elength : 1"
match = re.search(r'(>Bounded_RNA_of:(\w+):(\w)length : 1)',seq)
print match.group()

# prints this:
# >Bounded_RNA_of:1DDL:Elength : 1

有什么方法可以做到?

最佳答案

这是由于全局捕捉括号,你应该只捕捉两个需要的元素。

import re
seq=">Bounded_RNA_of:1DDL:Elength : 1"
match = re.search(r'>Bounded_RNA_of:(\w+):(\w)length : 1',seq)
print match.group(1), match.group(2)

关于python - 使用 Python 的正则表达式捕获字符串的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24280607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com