找不到值,看错误

时间:2014-08-03 06:52:19

标签: python regex

我正在编码在两个文本字符串之间打印,但是我收到了错误:

正则表达式编码:

import re
with open("in.txt") as f:
   lines = f.read()
   m = re.findall(r'(?s)(?<=Credit\s*\b).*?(?=Amount)', lines) 
   for i in m:
       print i

(返回看后面未找到)

另一种编码:

with open("in.txt") as f:
    lines = f.read()
    cred_ind = (lines.index("Credit"))
    am_ind = lines.index("Amount")
    print(lines[cred_ind+6:am_ind])

(返回未找到子字符串)

文字档案:

....

accounts
Bank
Credit
 good value
 money
Amount  
Amount

输出:

good value
money

1 个答案:

答案 0 :(得分:0)

Pythons re模块不支持可变长度的lookbehind。

简单修复,避免使用外观断言..

m = re.findall(r'(?s)Credit\s*(.*?)Amount', lines)