Linux教程網
Python正則表達式測試:
- import re
- import sys
- if len(sys.argv) < 3:
- print("lack of argument\nusage: python reg.py passwd.txt")
- sys.exit()
- #keyword=re.compile(r'''travel+ed''')
- keyword=re.compile(sys.argv[2])
- aa=open(sys.argv[1],"r")
- bb=aa.readlines()
- #print(bb)
- for line in bb:
- #print(line)
- data=re.findall(keyword,line)
- #print("data:%s,length:%d,type:%s" %(data,len(data),type(data)))
- if len(data)>0:
-
- for i in range(0,len(data)):
- if type(data[0])=="builtins.str":
- print(data[i])
-
- aa.close()
Copyright ©
Linux教程網 All Rights Reserved