Python正则表达式

(1)认为写的比较好的python正则表达式指南

http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html

(2)正则匹配中关于单行模式以及多行模式的一些说明

摘自

http://hi.baidu.com/jiyeqian/blog/item/4ffe5d18ab3aa61735fa4198.html

行模式主要用于改变点号的匹配规则,而多行模式用于改变 ^ 和 $ 的匹配规则;默认情况下单行模式和多行模式均处于 off 状态,可以分别激活或同时激活这两种模式。

开启单行模式:(?s);开启多行模式:(?m)

默认情况下,点号能够匹配除换行符外的所有字符,如果想要匹配到换行符的话,就需要激活单行模式,单行模式下点号就能匹配天地间的一切字符了,所以余大将之译为点号通配模式。

而 ^ 和 $ 在默认情况下是匹配整个目标文本起始和结束的锚,也就是说等价于 \A 和 \Z 的,如果激活了多行模式的话,^ 和 $ 就变成了能够匹配到行起始行结束的锚。

所以,最后说一句,单行模式和多行模式之间,一毛钱关系都没有

________________________________________________________________________

Python 正则表达式的 MULTILINE 模式


When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline); and the pattern character '$' matches at the end of the string and at the end of each line (immediately preceding each newline). 

By default, '^' matches only at the beginning of the string, and '$' only at the end of the string and immediately before the newline (if any) at the end of the string.


例如(须import re):

print re.findall('^From''Reciting \nFrom Memory') 将返回:[]

print re.findall('^From', 'Reciting \nFrom Memory', re.M) 将返回:['From']

\A 仅匹配字符串的开头。当不在MULTILINE模式中时,\A和^是作用相同的。在MULTILINE模式中,它们是不同的;\A仍然仅匹配字符串的开头,但是^也匹配'\n'或'\r'之后的位置。 

参考:
http://docs.python.org/library/re.html#re.MULTILINE
http://wufoxfm95.javaeye.com/blog/160775

原文地址:https://www.cnblogs.com/finallyliuyu/p/2313395.html