地址:http://docs.python.org/library/strings.html
8.1 常见的string操作
介绍了一些常量。例如 string.ascii_letters代表所有的大小写英文字幕。下面讲解了字符串的格式化操作,看起来比较吃力,近期应用的可能也不大,越过。
8.2 正则表达式
介绍了正则表达式的语法和使用方法。
^代表字符串的开始或者一行的开始,$相反。
*、+、?、 *?、 +?、??、{m}、{m,n} 的个数含义。
/转义,[]是字符组。“|”是或。
()是一个组。
(?...)是一个扩展标记法,除了(?P<name>...)外,不代表一个组。
(?P<name>...)建立一个组的同时,让后续程序可以通过m.group('name')访问,在统一正则内使用(?P=name)来匹配之前的小组匹配的内容。Django匹配URL的时候就可以使用这个功能。
/可以用来匹配特殊字符,例如/$匹配$。
/number 匹配之前的小组。
re模块的内容。
re.compile (pattern [ , flags ] ),flags为re.I(不区分大消息)和re.M(多行)等。此外还有search, match, split, findall, sub等。
正则表达式对象
编译后的正则支持的方法,例如 RegexObject.match (string [ , pos [ , endpos ]] ), RegexObject.search (string [ , pos [ , endpos ]] )等等。
Match对象
MatchObject.expand (template )、 MatchObject.group ([ group1 , ... ] )等等。
Python正则表达式教程:
http://www.amk.ca/python/howto/regex/regex.html
http://www.diveintopython.org/regular_expressions/index.html
2009年6月3日23:45:42
使用正则表达式
1. 编译一个正则
>>> import re
>>> regex = re.compile(r'1.*')
>>> regex
<_sre.SRE_Pattern object at 0x014DA520>
2. 匹配
>>> m = regex.match('1234')
>>> m.group()
'1234'
>>> m.start()
0
>>> m.end()
4
>>> m.span()
(0, 4)
#-------- 匹配失败 ---------
>>> m = regex.match('234')
>>> m.group()
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
m.group()
AttributeError: 'NoneType' object has no attribute 'group'
>>> print m
None
#--------match 只检查字符串的开始处 ------------
>>> m = regex.match('222215')
>>> print m
None
3. 搜索
>>> s = regex.search('222215')
>>> print s
<_sre.SRE_Match object at 0x01403DB0>
>>> s.group()
'15'
>>> s.span()
(4, 6)
4. 将字符串按照单词分隔
#--------/W+ 匹配一个以上的非单词字符 -------------
>>> s = 'This is a test of regular expression.'
>>> r = re.compile(r'/W+')
>>> r.split(s)
['This', 'is', 'a', 'test', 'of', 'regular', 'expression', '']
2009 年 6 月 4 日 9:28:41
8.3 struct
处理 C 语言里的 struct 。 WuErPIng 有一篇日志有更方便的处理方法。
8.4 difflib
比较两个序列( sequences )的不同。
例如 HtmlDiff 可以产生一个 table 来描述两个文件不同。
>>> s1 = '1'
>>> s2 = '123'
>>> t = HtmlDiff().make_table(s1,s2)
>>> print t
<table class="diff" id="difflib_chg_to1__top"
cellspacing="0" cellpadding="0" rules="groups" >
<colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
<colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
<tbody>
<tr><td class="diff_next" id="difflib_chg_to1__0"><a href="#difflib_chg_to1__0">f</a></td><td class="diff_header" id="from1_1">1</td><td nowrap="nowrap">1</td><td class="diff_next"><a href="#difflib_chg_to1__0">f</a></td><td class="diff_header" id="to1_1">1</td><td nowrap="nowrap">1</td></tr>
<tr><td class="diff_next"><a href="#difflib_chg_to1__top">t</a></td><td class="diff_header"></td><td nowrap="nowrap"></td><td class="diff_next"><a href="#difflib_chg_to1__top">t</a></td><td class="diff_header" id="to1_2">2</td><td nowrap="nowrap"><span class="diff_add">2</span></td></tr>
<tr><td class="diff_next"></td><td class="diff_header"></td><td nowrap="nowrap"></td><td class="diff_next"></td><td class="diff_header" id="to1_3">3</td><td nowrap="nowrap"><span class="diff_add">3</span></td></tr>
</tbody>
</table>
2009 年 6 月 4 日 11:37:19