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:

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

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

phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))? # area code
(\s|-|\.)? # separator
\d{3} # first 3 digits
(\s|-|\.) # separator
\d{4} # last 4 digits
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
)''', 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. php学习手记

    在学习常量的时候,一直觉得奇怪 为啥常量的时候总是有个“.”在常量的后面,经过学习现在得知该点是 相当于Java当中的“+”用于连接字符 <?php header("Content-t ...

  2. 配置163Yum源自动判断你的系统是Centos版本(适用于5.x或6.x)

    #!/bin/bash #Author:nulige #Date: 2015-3-8 #实现功能:自动判断你的系统是Centos版本,适用于5.x或6.x mv /etc/yum.repos.d/Ce ...

  3. Maven的内置变量

    Maven内置变量说明: ${basedir} 项目根目录(即pom.xml文件所在目录) ${project.build.directory} 构建目录,缺省为target目录 ${project. ...

  4. ubuntu14.04完全卸载mysql

    1.删除 mysql1 sudo apt-get autoremove --purge mysql-server-5.0 2 sudo apt-get remove mysql-server 3 su ...

  5. [转] Java序列化与反序列化

    原文地址:http://blog.csdn.net/wangloveall/article/details/7992448 Java序列化与反序列化是什么?为什么需要序列化与反序列化?如何实现Java ...

  6. Shell脚本_启动停止重启sh脚本

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  7. Maven-生命周期

    Maven的生命周期是为了对所有的构建过程进行了抽象了,便于统一. clean(清理) 此生命周期旨在给工程做清理工作,它主要包含以下阶段: pre-clean - 执行项目清理前所需要的工作. cl ...

  8. Js-字符串截取substring,分割split,指标indexOf,拼接John

    函数:split() 功能:使用一个指定的分隔符把一个字符串分割存储到数组例子: var theString=”jpg|bmp|gif|ico|png”; var arr=theString.spli ...

  9. 一起学HTML基础-利用CSS和JavaScript制作一个切换图片的网页

    由于个人原因,不详细写步骤 思路: 一.布局 二.制作图片区和按钮区的div及颜色.边框.背景属性等 三.用PS将四张图片剪切到同一个尺寸,重叠放置在图片切换区,透明度设置为0 四.点击对应按钮时,将 ...

  10. 安装python-docx

    安装环境: windows 7 64位 python 3.4.2 64位 说明: 用pip install python-docx不行,当装lxml时出现错误,一直卡在那. 安装非官方的版本,如下图, ...