我想在Python中使用正则表达式在字符串中找到匹配的字符串。该string
看起来是这样的:python正则表达式找到匹配的字符串
band 1 # energy -53.15719532 # occ. 2.00000000
ion s p d tot
1 0.000 0.995 0.000 0.995
2 0.000 0.000 0.000 0.000
tot 0.000 0.996 0.000 0.996
band 2 # energy -53.15719532 # occ. 2.00000000
ion s p d tot
1 0.000 0.995 0.000 0.995
2 0.000 0.000 0.000 0.000
tot 0.000 0.996 0.000 0.996
band 3 # energy -53.15719532 # occ. 2.00000000
我的目标是要找到tot
后的字符串。所以匹配的字符串会是这样的:
['0.000 0.996 0.000 0.996',
'0.000 0.996 0.000 0.996']
这里是我当前的代码:
pattern = re.compile(r'tot\s+(.*?)\n', re.DOTALL)
pattern.findall(string)
但是,输出给了我:
['1 0.000 0.995 0.000 0.995',
'0.000 0.996 0.000 0.996',
'1 0.000 0.995 0.000 0.995',
'0.000 0.996 0.000 0.996']
的我在做什么任何想法错误?
这解决了我的问题。我想我对“DOTALL”和“MUTILINE”感到困惑。需要阅读更多关于它的信息。 –