码迷,mamicode.com
首页 > 编程语言 > 详细

Python Regular expressions 正则表达式学习笔记

时间:2015-12-02 00:46:23      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

//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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!