标签:
正则表达式一般是爬虫用的,下面我们就通过一题多解来学习下正则表达式
参考:《Python核心编程(3rd)》—P33
1 # coding: utf-8 2 3 import re 4 5 data = """ 6 System 4 Services 0 388 K 7 smss.exe 408 Services 0 1,440 K 8 System Idle Process 0 Services 0 24 K 9 """ 10 11 data = data.split("\n") 12 13 for i in data: 14 # (?: )与 ‘|‘ 15 print re.findall(r"((?:\w+.)+)\s\s+(\d+) \w+\s\s+\d\s\s+(\d+ K|\d+,\d+ K)", i) 16 # (?: )与 ‘(?:|)‘ 17 #print re.findall(r"((?:\w+.)+)\s\s+(\d+) \w+\s\s+\d\s\s+((?:\d+|\d+,\d+) K)", i) 18 # (?: )与 ‘[]‘ 19 #print re.findall(r"((?:\w+.)+)\s\s+(\d+) \w+\s\s+\d\s\s+([\d+,]+ K)", i) 20
结果如下:
我们来解释下(?:)的用法,大家再看一下下面的例子,就清楚下
标签:
原文地址:http://www.cnblogs.com/Ruby517/p/5792727.html