需求:
查找多行中含有sshd的行
Python:
import re
subject = '''build 22873 0.0 0.0 138964 2348 ? S 10:29 0:00 sshd: build@pts/0
... build 22874 1.2 0.0 28320 9564 pts/0 Ss 10:29 0:00 -bash
... build 22997 0.0 0.0 4400 612 ? S 10:29 0:00 /bin/sh -xe /tmp/hudson3230150410476111992.sh
... build 22999 0.2 0.0 39492 3096 ? Sl 10:29 0:00 expect /home/build/3290/release/pica8/build.tcl build /home/build/3290/release/pica8/branche
... build 23002 0.0 0.0 41528 2884 pts/1 Ss+ 10:29 0:00 ssh build@10.10.50.16
... root 23005 0.4 0.0 136744 6224 ? Ss 10:29 0:00 sshd: build [priv]
... build 23130 0.0 0.0 136744 2324 ? S 10:29 0:00 sshd: build@pts/22
... build 23131 8.6 0.0 28312 9548 pts/22 Ss 10:29 0:00 -bash
... build 23251 1.2 0.0 118504 3696 pts/22 D+ 10:29 0:00 svn cleanup ../../sdk/sdk-xgs-robo-6.'''
lines = re.split("\r?\n",subject)
reobj = re.compile("sshd")
for line in lines[:]:
if reobj.search(line):
print '%s match!' % line
else:
print '%s no match!' % line
原文链接:https://www.f2er.com/regex/361651.html