1 #!/usr/bin/env python
2
3 from cgi import FieldStorage
4 from os import environ
5 from cStringIO import StringIO
6 from urllib import quote, unquote
7 from string import capwords, strip, split, join
8
9 class AdvCGI(object):
10
11 header = 'Content-Type: text/html\n\n'
12 url = '/py/advcgi.py'
13
例如将1,2,3,,,,,13,,去掉,用python脚本处理?求解!
#-*- coding: utf-8 -*- import re s = ''' 1 #!/usr/bin/env python 2 3 from cgi import FieldStorage 4 from os import environ 5 from cStringIO import StringIO 6 from urllib import quote, unquote 7 from string import capwords, strip, split, join 8 9 class AdvCGI(object): 10 11 header = 'Content-Type: text/html\n\n' 12 url = '/py/advcgi.py' 13 ''' LINE_PATTERN =r'\s*\d+\s?(.*)' def func(text): c = re.compile(LINE_PATTERN) lists = [] lines = text.split('\n') for line in lines: r = c.findall(line) if r: lists.append(r[0]) return '\n'.join(lists) if __name__ == '__main__': l = func(s) print l
LINE_PATTERN =r'\s*\d+\s?(.*)'能给我详细讲讲这个正则表达式吗? \s 是匹配任何空白字符,*匹配前面的正则出现0次或多次,
\d匹配数字,+表示数字出现一次或多次。
我不明白的是,r = c.findall('2 v=[] \n') 后为什么r=['v=[] '],它是如何实现将数字去掉的?
@forwhy:
LINE_PATTERN =r'\s*\d+\s?(.*)' 这个正则的最终获取结果就是括号 (.*) 中的内容,前面的正则你既然看懂了,那后面的 (.*) 不包括前面的数字也就好懂了