gpt4 book ai didi

python - python 中的 re.search 正则表达式不起作用

转载 作者:行者123 更新时间:2023-12-01 01:54:49 24 4
gpt4 key购买 nike

我想为字符串编写一个正则表达式 test = "ID=ss59537-RA:exon:0;Parent=ss59537-RA;" 我所以有了这个 searchstr = re.compile(r'(ID = ss[\d]+-RA)(:)(外显子:[\d]+)(;)(父级 = ss[\d]+-RA;)') 但当我尝试运行 re.search 命令时,我没有得到任何结果。我在这里做错了什么?

searchstr = re.compile(r'(ID = ss[\d]+-RA)(:)(exon:[\d]+)(;)(Parent = ss[\d]+-RA;)')
test = "ID=ss59537-RA:exon:0;Parent=ss59537-RA;"
match = re.search(searchstr, test)
print(match)

我确保正则表达式与字符串匹配,但是当我使用 reg.search 运行它时,它不起作用。

最佳答案

您似乎计划在 = 符号周围留出任意数量的空格。您可以使用 \s* 而不是文字空格来匹配任何 0 个空白字符。我还建议从单个原子周围删除 [] ([\d] = \d),并且将最后一个 ) 移到 ; 之前:

import re
searchstr = re.compile(r'(ID\s*=\s*ss\d+-RA):(exon:\d+);(Parent\s*=\s*ss\d+-RA);')
test = "ID=ss59537-RA:exon:0;Parent=ss59537-RA;"
match = re.search(searchstr, test)
print(match.groups())
# => ('ID=ss59537-RA', 'exon:0', 'Parent=ss59537-RA')

请参阅Python demo .

关于python - python 中的 re.search 正则表达式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50338758/

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