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

标签 python regex

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

>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/

相关文章:

python - 如何在 django-rest-framework 查询集响应中添加注释数据?

python - 直接调用时如何使python对象返回属性数据

python - 打开元素包装并添加新元素

regex - 删除 IntelliJ 中文本周围的引号(使用正则表达式?)

regex - 使用str.replace从pandas中的字符串中删除括号

JavaScript:使用decodeUri或unescape不会删除空格

python - 可插入的Python程序

python - 在循环定义的字典上使用 `==` 运算符

javascript - 正则表达式检测带括号的双引号 javascript 对象属性

c# - SQL 查询的正则表达式给出空的 MatchCollection