[英]Python: use string variable as search pattern in regex
I'm trying to search a nucleotide sequence (composed of only A,C,G,T) for a user-defined pattern, using regex: 我正在尝试使用正则表达式搜索用户定义的模式的核苷酸序列(仅由A,C,G,T组成):
The relevant code is as follows: 相关代码如下:
match = re.match(r'{0}'.format(pattern), sequence)
match always returns None, where I need it to return the part of the sequence that matches the user query... match总是返回None,我需要它来返回与用户查询匹配的部分序列...
What am I doing wrong? 我究竟做错了什么?
EDIT: This is how I constructed the search pattern: 编辑:这就是我构造搜索模式的方式:
askMotif = raw_input('Enter a motif to search for it in the sequence (The wildcard character ‘?’ represents any nucleotide in that position, and * represents none or many nucleotides in that position.): ')
listMotif= []
letterlist = ['A','C','G','T', 'a', 'c','g','t']
for letter in askMotif:
if letter in letterlist:
a = letter.capitalize()
listMotif.append(a)
if letter == '?':
listMotif.append('.')
if letter == '*':
listMotif.append('*?')
pattern = ''
for searcher in listMotif:
pattern+=searcher
Not very pythonic, I know... 不是很pythonic,我知道...
That should work fine: 那应该工作正常:
>>> tgt='AGAGAGAGACGTACACAC'
>>> re.match(r'{}'.format('ACGT'), tgt)
>>> re.search(r'{}'.format('ACGT'), tgt)
<_sre.SRE_Match object at 0x10a5d6920>
I think it may because you mean to use search vs match 我认为这可能是因为您是要使用搜索还是匹配
Hint on your posted code: 提示您发布的代码:
prompt='''\
Enter a motif to search for it in the sequence
(The wildcard character '?' represents any nucleotide in that position,
and * represents none or many nucleotides in that position.)
'''
pattern=None
while pattern==None:
print prompt
user_input=raw_input('>>> ')
letterlist = ['A','C','G','T', '?', '*']
user_input=user_input.upper()
if len(user_input)>1 and all(c in letterlist for c in user_input):
pattern=user_input.replace('?', '.').replace('*', '.*?')
else:
print 'Bad pattern, please try again'
re.match()
only matches at the beginning of the sequence. re.match()
仅在序列的开头匹配。 Perhaps you need re.search()
? 也许您需要re.search()
?
>>> re.match(r'{0}'.format('bar'), 'foobar').group(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> re.search(r'{0}'.format('bar'), 'foobar').group(0)
'bar'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.