<p>我的正则表达式搜索有点困难,我不知道为什么。我有一个文件,其值的格式如下:</p>
<pre><code> 1 -1 2 SER HA H 4.477 0.003 1
2 -1 2 SER HB2 H 3.765 0.001 1
3 -1 2 SER HB3 H 3.765 0.001 1
4 -1 2 SER C C 173.726 0.2 1
5 -1 2 SER CA C 58.16 0.047 1
6 -1 2 SER CB C 64.056 0.046 1
7 0 3 HIS H H 8.357 0.004 1
8 0 3 HIS HA H 4.725 0.003 1
9 0 3 HIS HB2 H 3.203 0.003 2
.....
63 7 10 GLU HA H 4.328 0.004 1
64 7 10 GLU HB2 H 2.154 0.005 2
65 7 10 GLU HB3 H 2.156 0.004 2
66 7 10 GLU HG2 H 2.262 0.014 2
67 7 10 GLU HG3 H 2.464 0.001 2
68 7 10 GLU C C 177.242 0.2 1
69 7 10 GLU CA C 59.009 0.068 1
...
</code></pre>
<p>我想逐行搜索上面的字符串</p>
<pre><code>import re
with open('delete.txt') as file:
for lines in file:
modifier=lines.strip()
A=re.search('\B\d+\s[A-Z][A-Z][A-Z]\s[A-Z]',modifier)
if A != None:
search=A.string
print(search)
</code></pre>
<p>上述文件的格式变化很大,但始终保持一致的是会有一个数字,后面跟着3个字母,后面跟着另一个字母。即<code>2 SER HA</code></p>
<p>所以我决定用它作为我的正则表达式搜索,但这不太管用。在<code>63 7 10 GLU</code>行之后,它工作得很好,但是它找不到之前的任何其他条目,尽管事实上每一行都是相同的格式</p>
<p>上面的示例是一个MVE</p>
<p>任何帮助都将不胜感激</p>
<pre><code>import re
fhand=open('delete.txt')
for line in fhand:
inp=line.rstrip()
x=re.findall('^\d\s\S\d\s(\d\s\S+\s\S+)',inp)
if len(x) >0:
print(x)
</code></pre>