re正则表达式16_managing complex regexes
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的更多相关文章
- win7下,使用django运行django-admin.py无法创建网站
安装django的步骤: 1.安装python,选择默认安装在c盘即可.设置环境变量path,值添加python的安装路径. 2.下载ez_setup.py,下载地址:http://peak.tele ...
- 【读书笔记】iOS-正则表达式
正则表达式通常称为regexes,是文本处理中模式匹配的一个标准,也是处理字符串的一个强有力的工具.使用正则表达式时,需要指定一个字符串作为模式串去检索目标字符串.你可以使用正则表达式来查找字符串中匹 ...
- Python 正则表达式入门(初级篇)
Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. 转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达 ...
- Python::re 模块 -- 在Python中使用正则表达式
前言 这篇文章,并不是对正则表达式的介绍,而是对Python中如何结合re模块使用正则表达式的介绍.文章的侧重点是如何使用re模块在Python语言中使用正则表达式,对于Python表达式的语法和详细 ...
- 15个超实用的php正则表达式
在这篇文章里,我已经编写了15个超有用的正则表达式,WEB开发人员都应该将它收藏到自己的工具包. 验证域名 检验一个字符串是否是个有效域名. $url = "http://komunitas ...
- PHP正则表达式及实例
PHP正则表达式及实例 博客分类: Php / Pear / Mysql / Node.js 正则表达式PHPWordPressFPApache 关联: 正则表达式 去除连续空白 + 获取url + ...
- 正则表达式(BREs,EREs,PREs)差异比较
我想各位也和我一样,再linux下使用grep,egrep, awk , sed, vi的搜索时,会经常搞不太清楚,哪此特殊字符得使用转义字符'\' .. 哪些不需要, grep与egrep的差异 ...
- 10个实用的PHP正则表达式 (转)
http://www.iteye.com/news/23231 1. 验证E-mail地址 这是一个用于验证电子邮件的正则表达式.但它并不是高效.完美的解决方案.在此不推荐使用. $email = & ...
- 10个实用的PHP正则表达式
正则表达式是程序开发中一个重要的元素,它提供用来描述或匹配文本的字符串,如特定的字符.词或算式等.但在某些情况下,用正则表达式去验证一个字符串比较复杂和费时.本文为你介绍10种常见的实用PHP正则表达 ...
随机推荐
- python基础-基本数据类型总结_整型(int)_字符型(str)_day3
一.基本数据类型 1.整型(int) ps1:数字 int ,所有的功能,都放在int里a1 = 123a1 = 456 ps2: int 将字符串转换为数字 # -int # 将字符串转换为数字 ...
- AngularJS 日期转换字符串
日期转换成字符串的办法有很多种,其中最简单的方法是 使用AngularJS的filter来实现. $filter('date')(date, 'yyyyMM'): $filter('date')(da ...
- python练习之购物车脚本
# -*- coding:utf-8 -*- #简单的购物车小程序 author:李瑞鑫 shopping_car =[] product_list_tatol = "---product ...
- LRU设计
list是双向链表,map保存key对应到list中的迭代器的位置,list保存<key,value> class LRUCache{ public: LRUCache(int capac ...
- 利用SpringAOP 实现 日志输出
目的: 需要对一些事物的操作进行日志记录,如果在service内进行记录,大量的代码重复,并且维护比较麻烦.所以采用AOP的方式对service进行拦截.使用自定义注解的目的则是判断是否需要记录日志和 ...
- bzoj1503
treap改了好长时间,erase写错了... #include<iostream> #include<cstdio> #include<cstdlib> usin ...
- git log 常用命令及技巧
git log常用命令以及技巧 1.git log 如果不带任何参数,它会列出所有历史记录,最近的排在最上方,显示提交对象的哈希值,作者.提交日期.和提交说明.如果记录过多,则按Page Up.Pag ...
- git初体验(一)基础
一.window下的git安装 1.安装教程 网上教程一堆,我参考的是这个:Git_Windows 系统下Git安装图解 还有这个也不错 2.环境搭建: 在配置完成后,自动加载到系统环境变量中,如我的 ...
- 安装完ActivePython后Python的Idle窗口打不开也卸载不掉的解决方法
1.想找一个好的PythonIDE开发环境所以就安装了ActivePython开发公具,结果发现软件打不开,机器上装的Python环境也不能用了,网上相关的解决方法也是寥寥无几...真悲催! 2.后来 ...
- epoll
https://segmentfault.com/a/1190000003063859http://man7.org/linux/man-pages/man7/epoll.7.html EPOLLIN ...