相关疑难解决方法(0)

匹配python正则表达式中的整行

在文本文件中考虑此输入:

foo
bar
foobar
Run Code Online (Sandbox Code Playgroud)

如果我查看python API的重包,我明白如果我想匹配foo而不是foobar我明白这个代码应该这样做

import re

code = open ('play.txt').read()
print code

print re.findall('^foo$',code)
Run Code Online (Sandbox Code Playgroud)

但输出读数

Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import play
foo
bar
foobar

[]
>>> 
Run Code Online (Sandbox Code Playgroud)

为什么?

python regex

2
推荐指数
1
解决办法
92
查看次数

标签 统计

python ×1

regex ×1