Managing Complex Regexes

Regular expressions are fine if the text pattern you need to match is simple. But matching complicated text patterns might require long, convoluted regular expressions. You can mitigate this by telling the re.compile() function to ignore whitespace and comments inside the regular expression string. This “verbose mode” can be enabled by passing the variable re.VERBOSE as the second argument tore.compile().

Now instead of a hard-to-read regular expression like this:

  1. phoneRegex = re.compile(r'((\d{3}|\(\d{3}\))?(\s|-|\.)?\d{3}(\s|-|\.)\d{4}
  2. (\s*(ext|x|ext.)\s*\d{2,5})?)')

you can spread the regular expression over multiple lines with comments like this:

  1. phoneRegex = re.compile(r'''(
  2. (\d{3}|\(\d{3}\))? # area code
  3. (\s|-|\.)? # separator
  4. \d{3} # first 3 digits
  5. (\s|-|\.) # separator
  6. \d{4} # last 4 digits
  7. (\s*(ext|x|ext.)\s*\d{2,5})? # extension
  8. )''', re.VERBOSE)

Note how the previous example uses the triple-quote syntax (''') to create a multiline string so that you can spread the regular expression definition over many lines, making it much more legible.

The comment rules inside the regular expression string are the same as regular Python code: The # symbol and everything after it to the end of the line are ignored. Also, the extra spaces inside the multiline string for the regular expression are not considered part of the text pattern to be matched. This lets you organize the regular expression so it’s easier to read.

 

re正则表达式16_managing complex regexes的更多相关文章

  1. win7下,使用django运行django-admin.py无法创建网站

    安装django的步骤: 1.安装python,选择默认安装在c盘即可.设置环境变量path,值添加python的安装路径. 2.下载ez_setup.py,下载地址:http://peak.tele ...

  2. 【读书笔记】iOS-正则表达式

    正则表达式通常称为regexes,是文本处理中模式匹配的一个标准,也是处理字符串的一个强有力的工具.使用正则表达式时,需要指定一个字符串作为模式串去检索目标字符串.你可以使用正则表达式来查找字符串中匹 ...

  3. Python 正则表达式入门(初级篇)

    Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. 转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达 ...

  4. Python::re 模块 -- 在Python中使用正则表达式

    前言 这篇文章,并不是对正则表达式的介绍,而是对Python中如何结合re模块使用正则表达式的介绍.文章的侧重点是如何使用re模块在Python语言中使用正则表达式,对于Python表达式的语法和详细 ...

  5. 15个超实用的php正则表达式

    在这篇文章里,我已经编写了15个超有用的正则表达式,WEB开发人员都应该将它收藏到自己的工具包. 验证域名 检验一个字符串是否是个有效域名. $url = "http://komunitas ...

  6. PHP正则表达式及实例

    PHP正则表达式及实例 博客分类: Php / Pear / Mysql / Node.js 正则表达式PHPWordPressFPApache  关联: 正则表达式 去除连续空白 + 获取url + ...

  7. 正则表达式(BREs,EREs,PREs)差异比较

    我想各位也和我一样,再linux下使用grep,egrep, awk , sed, vi的搜索时,会经常搞不太清楚,哪此特殊字符得使用转义字符'\' ..   哪些不需要, grep与egrep的差异 ...

  8. 10个实用的PHP正则表达式 (转)

    http://www.iteye.com/news/23231 1. 验证E-mail地址 这是一个用于验证电子邮件的正则表达式.但它并不是高效.完美的解决方案.在此不推荐使用. $email = & ...

  9. 10个实用的PHP正则表达式

    正则表达式是程序开发中一个重要的元素,它提供用来描述或匹配文本的字符串,如特定的字符.词或算式等.但在某些情况下,用正则表达式去验证一个字符串比较复杂和费时.本文为你介绍10种常见的实用PHP正则表达 ...

随机推荐

  1. fstream 中判断是否成功打开文件

    from: http://blog.csdn.NET/zhtsuc/article/details/2938614 关于C++ fstream的一个容易使用出错的地方 关于c++ 中 文件流的两个类, ...

  2. [Bundling and Minification ] 一、如何绑定

    绑定和压缩(缩小)是ASP.NET 4.5出现的用来提高程序性能的两个重要的技术.绑定(Bundling)是将多个文件合并为一个文件,压缩(Minification)主要是将文件缩小,如Js .CSS ...

  3. [转]div与span区别及用法

    DIV与SPAN区别及div与san用法篇 接下来了解在div+css开发的时候在html网页制作,特别是标签运用中div和span的区别及用法.新手在使用web标准(div css)开发网页的时候, ...

  4. 【转载】java版打字练习软件

    网上找到一个java版的打字的游戏 import java.applet.Applet; import java.applet.AudioClip; import java.awt.Dimension ...

  5. poj2553 强连通缩点

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10114   Accepted: ...

  6. 【hdu 5918】Sequence I(KMP)

    给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 # ...

  7. Invalid initial heap size: -Xms

    -Xxs512m注意 Xxs 和 512m中间无空格就行了.

  8. Matlab以特殊分隔符写入txt(dlmwrite)

    clc clear all load('E:\RSWeb\mahoyt数据集\movielens\u.data'); dlmwrite('E:\RSWeb\mahoyt数据集\movielens\ne ...

  9. Windows批处理以服务的方式启动解决思路(ShadowsockR注册成Windows Service)

    我以ShadowsockR的server启动来解释: 由于这东西是python,如果要启动,可以写一个批处理(python.exe server.py)来启动,但是我观察发现启动的时候是附带pytho ...

  10. 洛谷P2746 [USACO5.3]校园网Network of Schools

    题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学校的列表中. 你要写 ...