易截截图软件、单文件、免安装、纯绿色、仅160KB

python笔记之正则表达式

 正则表达式
具体的参考手册,这里记下一些小问题:
1、re对象的方法
match    Match a regular expression pattern to the beginning of a string.
search   re.search(pattern, string, flags) flags:re.I re.M re.X re.S re.L re.U
sub      Substitute occurrences of a pattern found in a string.
subn     Same as sub, but also return the number of substitutions made.
split    Split a string by the occurrences of a pattern.
findall  Find all occurrences of a pattern in a string.
finditer Return an iterator yielding a match object for each match.
iter = re.finditer("23", "123423523")
for i in iter:
print i.span() 
compile  将一个pattern编译成一个RegexObject.
       re对象的使用方法,reobject = re.complie(pattern),从reobject获得pattern的方法,使用reobject.pattern属性
       如果一个reobject要多次使用,最好使用compile方法提高性能,否则可以直接按这种方式使用,re.search(pattern, string)
purge    Clear the regular expression cache.
escape   Backslash all non-alphanumerics in a string.
2、要使group(s)方法,应该在pattern中对想要在tuples中的部分加上括号,例如“123-45”,想匹配这个string,但是只想获得前三个数字,则可以使用这样的pattern,"(\d{3})-\d{2}"
,注意前面使用了括号,再调用groups方法会得到tuples,("123",)
。group(s)是SRE_Match object 的方法,通常使用方法为reobject.search(string).group(s)
3、要匹配任意单个字符使用".",任意个字符使用".*"(包括0个),或者".+"(至少一个)。
4、括号的多种用法
    (?:)不放入groups中
    (?iLmsux) 为pattern加入I,L,M,S,U,X标志
    (?P<name>) 为group的一项加入别名
    (?P=name) 匹配在之前用name表示的部分表达式所匹配到的内容
    (?#comment) 注释
    test1(?=test2)  如果test1后面跟着te


相关文档:

Python入门的36个例子 之 26

源代码下载:下载地址在这里
# 029
aFile = file(r'C:\in.txt', 'r')
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
print line,
# end of while
aFile.close()

output:
>>>
This is the first line.
This is the second line.
This is ......

Python入门的36个例子 之 36

# 040
import time
try:
f = file('040_Finally.py')
while True:
line = f.readline()
if len(line) == 0:
break
time.sleep(0.33)
print line,
# end of while
finally:
f.close()
print 'Closed the file.'
# end of try
output:
> ......

在Windows上安装Python+MySQL 的常见问题及解决方法

验证是否已经安装了MySQLdb:
==========================================================
d:\usr\local\Python25>python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] onwin32
Type "help", "copyright", "credits" or "license" for ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号