python regexp

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)

If you want to search '\ten', you need use raw strings in pattern.  

regexp = re.complie('\\ten')

In this way, you have escape '\' . Other char - \t (tab) \n (a new line) ...

>>> r"Hello" == "Hello" True

>>> r"\the"  == "\\the" True

>>> r"\the" == "\the" False



展开阅读全文

没有更多推荐了,返回首页

应支付0元
点击重新获取
扫码支付

支付成功即可阅读