小编Gli*_*boy的帖子

Python正则表达式在文件行的开头搜索字符串

这是我的代码:

#!/usr/bin/python
import io
import re
f = open('/etc/ssh/sshd_config','r')
strings = re.search(r'.*IgnoreR.*', f.read())
print(strings)
Run Code Online (Sandbox Code Playgroud)

返回数据,但我需要特定的正则表达式匹配:例如:

^\s*[^#]*IgnoreRhosts\s+yes
Run Code Online (Sandbox Code Playgroud)

如果我将代码更改为:

strings = re.search(r'^IgnoreR.*', f.read())
Run Code Online (Sandbox Code Playgroud)

甚至

strings = re.search(r'^.*IgnoreR.*', f.read())
Run Code Online (Sandbox Code Playgroud)

我什么都没收到.我需要能够在perl中使用真正的正则表达式

python regex file-io

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

标签 统计

file-io ×1

python ×1

regex ×1