标签:
//search lines that start with the string “From:”
import re hand = open(‘mbox-short.txt‘) for line in hand: line = line.rstrip() if re.search(‘ˆFrom:‘, line) : print line
// match any of the strings “From:”, “Fxxm:”, “F12m:”, or “F!@m:”
1 import re 2 hand = open(‘mbox-short.txt‘) 3 for line in hand: 4 line = line.rstrip() 5 if re.search(‘ˆF..m:‘, line) : 6 print line
//search lines that start with “From:”, followed by one or more characters (“.+”), followed by an at-sign
1 import re 2 hand = open(‘mbox-short.txt‘) 3 for line in hand: 4 line = line.rstrip() 5 if re.search(‘ˆFrom:.+@‘, line) : 6 print line
//uses findall() to find the lines with email addresses in them,
1 import re 2 s = ‘Hello from csev@umich.edu to cwen@iupui.edu about the meeting @2PM‘ 3 lst = re.findall(‘\S+@\S+‘, s) 4 print lst
The output of the program would be:[‘csev@umich.edu‘, ‘cwen@iupui.edu‘]
The “\S+” matches as many non-whitespace characters as possible.
Python Regular expressions 正则表达式学习笔记
标签:
原文地址:http://www.cnblogs.com/peng-vfx/p/5011711.html