赞
踩
Simplest case -
- import re
- regexp = re.compile('hello')
- count = 0
- file = open('j.txt','r')
- for line in file.readlines() :
- if regexp.search(line) :
- count = count + 1
- file.close()
- print(count)
Now, if you want to use wildcard pattern.
- import re
- regexp = re.compile('(h|H)ello')
- count = 0
- file = open('j.txt','r')
- for line in file.readlines() :
- if regexp.search(line) :
- count = count + 1
- file.close()
- print(count)
regexp = re.complie('\\ten')
>>> r"Hello" == "Hello" True
>>> r"\the" == "\\the" True
>>> r"\the" == "\the" False
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。