zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    jsp获取当前目录下的文件和目录,获取windows盘符
    WAS常用配置的文件
    Was提示SOAP链接不上可能停止 但WAS已经启动
    git本地仓库提交到远程
    ubuntu1804时间相差8小时
    docker安装&卸载
    ubuntu 安装离线字典
    写给还没入门的网页设计师们
    简单10步,建立一个完美的商业网站
    不会配色?来看看网页设计中怎样使用柔和色调
  • 原文地址:https://www.cnblogs.com/finallyliuyu/p/2313395.html
Copyright © 2011-2022 走看看