`
desert3
  • 浏览: 2142196 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

Dive Into Python正则表达式

阅读更多
  • 不贪婪的限定符 *?、+?、?? 或 {m,n}?,尽可能匹配小的文本。
  • \S  非空字符
  • "."  标识任意字符非换行字符,可以通过设置编译参数来包含匹配换行字符
  • (a|b|c) 要么匹配 a ,要么匹配 b ,要么匹配 c 。
  • |  A|B 表示A或者B , AB为任意的正则表达式  另外|是非贪婪的如果A匹配,则不找B
  • 松散正则表达式:忽略空白,注释 re.VERBOSE
  • ^ 匹配字符串的开始。
  • $ 匹配字符串的结尾。
  • \b 匹配一个单词的边界。
  • \d 匹配任意数字。
  • \D 非 \d,匹配任意非数字字符。
  • \s  表示空字符
  • \w  [a-zA-Z0-9_]
  • \W  非 \w
  • x? 匹配一个可选的 x 字符 (换言之,它匹配 1 次或者 0 次 x 字符)。
  • x* 匹配0次或者多次 x 字符。
  • x+ 匹配1次或者多次 x 字符。
  • x{n,m} 匹配 x 字符,至少 n 次,至多 m 次。
  • (x) 一般情况下表示一个记忆组 (remembered group) 。你可以利用 re.search 函数返回对象的 groups() 函数获取它的值。
  • []  表一系列字符  [abcd] 表a,b,c,d  [^a] 表示非a

re.sub(pattern,dest,string)
re.search(pattern,string)


电话号码正则表达式(松散正则表达式):
>>> phonePattern = re.compile(r'''
                # don't match beginning of string, number can start anywhere
    (\d{3})     # area code is 3 digits (e.g. '800')
    \D*         # optional separator is any number of non-digits
    (\d{3})     # trunk is 3 digits (e.g. '555')
    \D*         # optional separator
    (\d{4})     # rest of number is 4 digits (e.g. '1212')
    \D*         # optional separator
    (\d*)       # extension is optional and can be any number of digits
    $           # end of string
    ''', re.VERBOSE)
>>> phonePattern.search('work 1-(800) 555.1212 #1234').groups()        1
('800', '555', '1212', '1234')
>>> phonePattern.search('800-555-1212')                                2
('800', '555', '1212', '')
分享到:
评论

相关推荐

    dive into python3 (中文版)

    dive into python3 (中文版)dive into python3 (中文版)dive into python3 (中文版)

    Dive Into Python 3

    《深入Python 3》(Dive Into Python 3,英,)的的电子版,HTML格式

    Dive into Python 中文版

    Dive into Python 中文版 Dive into Python 中文版

    Python研究 从新手到高手 Dive Into Python 中文版

    Python 从新手到高手 Dive Into Python 是为有经验的程序员编写的一本 Python 书。 1.在多个平台安装Python 2.第一个Python程序 3.内置数据类型 4.自省的威力 5.对象和面向对象 6.异常和文件处理 7.正则表达式 8....

    dive into python_代码_教程

    dive into python_代码_教程 dive into python_代码_教程

    Dive into Python3

    Dive into Python3 很不错哦,有中文的就更好了。

    深入Python (Dive Into Python)

    深入python,深入Python (Dive Into Python) 译者序 by limodou 主页(http://phprecord.126.com) Python论坛 本书英文名字为《Dive Into Python》,其发布遵守 GNU 的自由文档许可证(Free Document Lience)的...

    diveintopython源代码

    diveintopython源代码 很全

    Dive Into Python 3 中文版.pdf

    Dive Into Python 3 中文版.pdf 如果需要英文的也可以联系我

    dive into python(中文版)

    dive into python的中文版,很不错的python教程

    diveintopython3

    diveintopython3 书中源代码,非常好的一本书,极力推介,本人从中学到很多写代码的技巧

    Dive Into Python 2 中文版

    Dive Into Python 2 中文版 深入学习Python,需要学习Python基础知识

    Dive Into Python 3, r870 (2010).pdf

    Didyoureadtheoriginal“DiveIntoPython”?Didyoubuyit onpaper?(Ifso,thanks!)AreyoureadytotaketheplungeintoPython3?…Ifso,readon.(Ifnoneofthat istrue,you’dbebetteroffstartingatthebeginning.) Python3...

    Dive into python

    dive into python英文原版,Dive Into Python 3 covers Python 3 and its differences from Python 2. Compared to Dive Into Python, it’s about 20% revised and 80% new material. The book is now complete, ...

    Dive Into Python 3 examples

    Dive Into Python 3 examples,随书示例代码包。

    Dive Into Python 3中文版

    Dive Into Python 是一份很知名的 Python 入门教程,由 Mark Pilgrim 编写,用户可以免费获取电子版本,而中文版则由啄木鸟社区翻译发布 [ 英文版 / 中文版 ] 。 前阵子,Mark Pilgrim 又发布了 《Dive into Python...

    dive into python3

    dive into python 3 英文版,很好懂,用具体例子讲述Python语法和一些编程技巧

    Dive Into Python 3 无水印pdf

    Dive Into Python 3 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Dive Into Python 3 中文版

    Dive Into Python 3 中文版

Global site tag (gtag.js) - Google Analytics