正则表达式是从信息中搜索特定的模式的一把瑞士军刀。它们是一个巨大的工具库,其中的一些功能经常被忽视或未被充分利用。今天我将向你们展示一些正则表达式的高级用法。

  举个例子,这是一个我们可能用来检测电话美国电话号码的正则表达式:

  1. r'^(1[-\s.])?(\()?\d{3}(?(2)\))[-\s.]?\d{3}[-\s.]?\d{4}$'

  我们可以加上一些注释和空格使得它更具有可读性。

  1. r'^'
  2. r'(1[-\s.])?' # optional '1-', '1.' or '1'
  3. r'(\()?' # optional opening parenthesis
  4. r'\d{3}' # the area code
  5. r'(?(2)\))' # if there was opening parenthesis, close it
  6. r'[-\s.]?' # followed by '-' or '.' or space
  7. r'\d{3}' # first 3 digits
  8. r'[-\s.]?' # followed by '-' or '.' or space
  9. r'\d{4}$' # last 4 digits

  让我们把它放到一个代码片段里:

  1. import re
  2.  
  3. numbers = [ "123 555 6789",
  4. "1-(123)-555-6789",
  5. "(123-555-6789",
  6. "(123).555.6789",
  7. "123 55 6789" ]
  8.  
  9. for number in numbers:
  10. pattern = re.match(r'^'
  11. r'(1[-\s.])?' # optional '1-', '1.' or '1'
  12. r'(\()?' # optional opening parenthesis
  13. r'\d{3}' # the area code
  14. r'(?(2)\))' # if there was opening parenthesis, close it
  15. r'[-\s.]?' # followed by '-' or '.' or space
  16. r'\d{3}' # first 3 digits
  17. r'[-\s.]?' # followed by '-' or '.' or space
  18. r'\d{4}$\s*',number) # last 4 digits
  19.  
  20. if pattern:
  21. print '{0} is valid'.format(number)
  22. else:
  23. print '{0} is not valid'.format(number)

  输出,不带空格:

  1. 123 555 6789 is valid
  2. 1-(123)-555-6789 is valid
  3. (123-555-6789 is not valid
  4. (123).555.6789 is valid
  5. 123 55 6789 is not valid

  正则表达式是 python 的一个很好的功能,但是调试它们很艰难,而且正则表达式很容易就出错。

  幸运的是,python 可以通过对 re.compile 或 re.match 设置 re.DEBUG (实际上就是整数 128) 标志就可以输出正则表达式的解析树。

  1. import re
  2.  
  3. numbers = [ "123 555 6789",
  4. "1-(123)-555-6789",
  5. "(123-555-6789",
  6. "(123).555.6789",
  7. "123 55 6789" ]
  8.  
  9. for number in numbers:
  10. pattern = re.match(r'^'
  11. r'(1[-\s.])?' # optional '1-', '1.' or '1'
  12. r'(\()?' # optional opening parenthesis
  13. r'\d{3}' # the area code
  14. r'(?(2)\))' # if there was opening parenthesis, close it
  15. r'[-\s.]?' # followed by '-' or '.' or space
  16. r'\d{3}' # first 3 digits
  17. r'[-\s.]?' # followed by '-' or '.' or space
  18. r'\d{4}$', number, re.DEBUG) # last 4 digits
  19.  
  20. if pattern:
  21. print '{0} is valid'.format(number)
  22. else:
  23. print '{0} is not valid'.format(number)

  解析树

  1. at_beginning
  2. max_repeat 0 1
  3. subpattern 1
  4. literal 49
  5. in
  6. literal 45
  7. category category_space
  8. literal 46
  9. max_repeat 0 2147483648
  10. in
  11. category category_space
  12. max_repeat 0 1
  13. subpattern 2
  14. literal 40
  15. max_repeat 0 2147483648
  16. in
  17. category category_space
  18. max_repeat 3 3
  19. in
  20. category category_digit
  21. max_repeat 0 2147483648
  22. in
  23. category category_space
  24. subpattern None
  25. groupref_exists 2
  26. literal 41
  27. None
  28. max_repeat 0 2147483648
  29. in
  30. category category_space
  31. max_repeat 0 1
  32. in
  33. literal 45
  34. category category_space
  35. literal 46
  36. max_repeat 0 2147483648
  37. in
  38. category category_space
  39. max_repeat 3 3
  40. in
  41. category category_digit
  42. max_repeat 0 2147483648
  43. in
  44. category category_space
  45. max_repeat 0 1
  46. in
  47. literal 45
  48. category category_space
  49. literal 46
  50. max_repeat 0 2147483648
  51. in
  52. category category_space
  53. max_repeat 4 4
  54. in
  55. category category_digit
  56. at at_end
  57. max_repeat 0 2147483648
  58. in
  59. category category_space
  60. 123 555 6789 is valid
  61. 1-(123)-555-6789 is valid
  62. (123-555-6789 is not valid
  63. (123).555.6789 is valid
  64. 123 55 6789 is not valid

  贪婪和非贪婪

  在我解释这个概念之前,我想先展示一个例子。我们要从一段 html 文本寻找锚标签:

  1. import re
  2.  
  3. html = 'Hello <a href="http://pypix.com" title="pypix">Pypix</a>'
  4.  
  5. m = re.findall('<a.*>.*<\/a>', html)
  6. if m:
  7. print m

  结果将在意料之中:

  1. ['<a href="http://pypix.com" title="pypix">Pypix</a>']

  我们改下输入,添加第二个锚标签:

  1. import re
  2.  
  3. html = 'Hello <a href="http://pypix.com" title="pypix">Pypix</a>' \
  4. 'Hello <a href="http://example.com" title"example">Example</a>'
  5.  
  6. m = re.findall('<a.*>.*<\/a>', html)
  7. if m:
  8. print m

  结果看起来再次对了。但是不要上当了!如果我们在同一行遇到两个锚标签后,它将不再正确工作:

  1. ['<a href="http://pypix.com" title="pypix">Pypix</a>Hello <a href="http://example.com" title"example">Example</a>']

  这次模式匹配了第一个开标签和最后一个闭标签以及在它们之间的所有的内容,成了一个匹配而不是两个 单独的匹配。这是因为默认的匹配模式是“贪婪的”。

当处于贪婪模式时,量词(比如 * 和 +)匹配尽可能多的字符。

  当你加一个问号在后面时(.*?)它将变为“非贪婪的”。

  1. import re
  2.  
  3. html = 'Hello <a href="http://pypix.com" title="pypix">Pypix</a>' \
  4. 'Hello <a href="http://example.com" title"example">Example</a>'
  5.  
  6. m = re.findall('<a.*?>.*?<\/a>', html)
  7. if m:
  8. print m

  现在结果是正确的。

  1. ['<a href="http://pypix.com" title="pypix">Pypix</a>', '<a href="http://example.com" title"example">Example</a>']

  前向界定符和后向界定符

  一个前向界定符搜索当前的匹配之后搜索匹配。通过一个例子比较好解释一点。

  下面的模式首先匹配 foo,然后检测是否接着匹配 bar

  1. import re
  2.  
  3. strings = [ "hello foo", # returns False
  4. "hello foobar" ] # returns True
  5.  
  6. for string in strings:
  7. pattern = re.search(r'foo(?=bar)', string)
  8. if pattern:
  9. print 'True'
  10. else:
  11. print 'False'

  这看起来似乎没什么用,因为我们可以直接检测 foobar 不是更简单么。然而,它也可以用来前向否定界定。 下面的例子匹配foo,当且仅当它的后面没有跟着 bar

  1. import re
  2.  
  3. strings = [ "hello foo", # returns True
  4. "hello foobar", # returns False
  5. "hello foobaz"] # returns True
  6.  
  7. for string in strings:
  8. pattern = re.search(r'foo(?!bar)', string)
  9. if pattern:
  10. print 'True'
  11. else:
  12. print 'False'

  后向界定符类似,但是它查看当前匹配的前面的模式。你可以使用 (?> 来表示肯定界定,(?<! 表示否定界定。

  下面的模式匹配一个不是跟在 foo 后面的 bar

  1. import re
  2.  
  3. strings = [ "hello bar", # returns True
  4. "hello foobar", # returns False
  5. "hello bazbar"] # returns True
  6.  
  7. for string in strings:
  8. pattern = re.search(r'(?<!foo)bar',string)
  9. if pattern:
  10. print 'True'
  11. else:
  12. print 'False'

  条件(IF-Then-Else)模式

  正则表达式提供了条件检测的功能。格式如下:

  1. (?(?=regex)then|else)

  条件可以是一个数字。表示引用前面捕捉到的分组。

  比如我们可以用这个正则表达式来检测打开和闭合的尖括号:

  1. import re
  2.  
  3. strings = [ "<pypix>", # returns true
  4. "<foo", # returns false
  5. "bar>", # returns false
  6. "hello" ] # returns true
  7.  
  8. for string in strings:
  9. pattern = re.search(r'^(<)?[a-z]+(?(1)>)$', string)
  10. if pattern:
  11. print 'True'
  12. else:
  13. print 'False'

  在上面的例子中,1 表示分组 (<),当然也可以为空因为后面跟着一个问号。当且仅当条件成立时它才匹配关闭的尖括号。

  条件也可以是界定符。

  无捕获组

  分组,由圆括号括起来,将会捕获到一个数组,然后在后面要用的时候可以被引用。但是我们也可以不捕获它们。

  我们先看一个非常简单的例子:

  1. import re
  2. string = 'Hello foobar'
  3. pattern = re.search(r'(f.*)(b.*)', string)
  4. print "f* => {0}".format(pattern.group(1)) # prints f* => foo
  5. print "b* => {0}".format(pattern.group(2)) # prints b* => bar

  现在我们改动一点点,在前面加上另外一个分组 (H.*)

  1. import re
  2. string = 'Hello foobar'
  3. pattern = re.search(r'(H.*)(f.*)(b.*)', string)
  4. print "f* => {0}".format(pattern.group(1)) # prints f* => Hello
  5. print "b* => {0}".format(pattern.group(2)) # prints b* => bar

  模式数组改变了,取决于我们在代码中怎么使用这些变量,这可能会使我们的脚本不能正常工作。 现在我们不得不找到代码中每一处出现了模式数组的地方,然后相应地调整下标。 如果我们真的对一个新添加的分组的内容没兴趣的话,我们可以使它“不被捕获”,就像这样:

  1. import re
  2. string = 'Hello foobar'
  3. pattern = re.search(r'(?:H.*)(f.*)(b.*)', string)
  4. print "f* => {0}".format(pattern.group(1)) # prints f* => foo
  5. print "b* => {0}".format(pattern.group(2)) # prints b* => bar

  通过在分组的前面添加 ?:,我们就再也不用在模式数组中捕获它了。所以数组中其他的值也不需要移动。

  命名组

  像前面那个例子一样,这又是一个防止我们掉进陷阱的方法。我们实际上可以给分组命名, 然后我们就可以通过名字来引用它们,而不再需要使用数组下标。格式是:(?Ppattern) 我们可以重写前面那个例子,就像这样:

  1. import re
  2. string = 'Hello foobar'
  3. pattern = re.search(r'(?P<fstar>f.*)(?P<bstar>b.*)', string)
  4. print "f* => {0}".format(pattern.group('fstar')) # prints f* => foo
  5. print "b* => {0}".format(pattern.group('bstar')) # prints b* => bar

  现在我们可以添加另外一个分组了,而不会影响模式数组里其他的已存在的组:

  1. import re
  2. string = 'Hello foobar'
  3. pattern = re.search(r'(?P<hi>H.*)(?P<fstar>f.*)(?P<bstar>b.*)', string)
  4. print "f* => {0}".format(pattern.group('fstar')) # prints f* => foo
  5. print "b* => {0}".format(pattern.group('bstar')) # prints b* => bar
  6. print "h* => {0}".format(pattern.group('hi')) # prints b* => Hello

  使用回调函数

  在 Python 中 re.sub() 可以用来给正则表达式替换添加回调函数。

  让我们来看看这个例子,这是一个 e-mail 模板:

  1. import re
  2. template = "Hello [first_name] [last_name], \
  3. Thank you for purchasing [product_name] from [store_name]. \
  4. The total cost of your purchase was [product_price] plus [ship_price] for shipping. \
  5. You can expect your product to arrive in [ship_days_min] to [ship_days_max] business days. \
  6. Sincerely, \
  7. [store_manager_name]"
  8. # assume dic has all the replacement data
  9. # such as dic['first_name'] dic['product_price'] etc...
  10. dic = {
  11. "first_name" : "John",
  12. "last_name" : "Doe",
  13. "product_name" : "iphone",
  14. "store_name" : "Walkers",
  15. "product_price": "$500",
  16. "ship_price": "$10",
  17. "ship_days_min": "1",
  18. "ship_days_max": "5",
  19. "store_manager_name": "DoeJohn"
  20. }
  21. result = re.compile(r'\[(.*)\]')
  22. print result.sub('John', template, count=1)

  注意到每一个替换都有一个共同点,它们都是由一对中括号括起来的。我们可以用一个单独的正则表达式 来捕获它们,并且用一个回调函数来处理具体的替换。

  所以用回调函数是一个更好的办法:

  1. import re
  2. template = "Hello [first_name] [last_name], \
  3. Thank you for purchasing [product_name] from [store_name]. \
  4. The total cost of your purchase was [product_price] plus [ship_price] for shipping. \
  5. You can expect your product to arrive in [ship_days_min] to [ship_days_max] business days. \
  6. Sincerely, \
  7. [store_manager_name]"
  8. # assume dic has all the replacement data
  9. # such as dic['first_name'] dic['product_price'] etc...
  10. dic = {
  11. "first_name" : "John",
  12. "last_name" : "Doe",
  13. "product_name" : "iphone",
  14. "store_name" : "Walkers",
  15. "product_price": "$500",
  16. "ship_price": "$10",
  17. "ship_days_min": "1",
  18. "ship_days_max": "5",
  19. "store_manager_name": "DoeJohn"
  20. }
  21. def multiple_replace(dic, text):
  22. pattern = "|".join(map(lambda key : re.escape("["+key+"]"), dic.keys()))
  23. return re.sub(pattern, lambda m: dic[m.group()[1:-1]], text)
  24. print multiple_replace(dic, template)

  不要重复发明轮子

  更重要的可能是知道在什么时候要使用正则表达式。在许多情况下你都可以找到 替代的工具。

  解析 [X]HTML

  Stackoverflow 上的一个答案用一个绝妙的解释告诉了我们为什么不应该用正则表达式来解析 [X]HTML。

  你应该使用使用 HTML 解析器,Python 有很多选择:

  后面两个即使是处理畸形的 HTML 也能很优雅,这给大量的丑陋站点带来了福音。

  ElementTree 的一个例子:

  1. from xml.etree import ElementTree
  2. tree = ElementTree.parse('filename.html')
  3. for element in tree.findall('h1'):
  4. print ElementTree.tostring(element)

  其他

  在使用正则表达式之前,这里有很多其他可以考虑的工具

  感谢阅读!

高级正则表达式技术(Python版)的更多相关文章

  1. 《大话设计模式》Python版代码实现

    上一周把<大话设计模式>看完了,对面向对象技术有了新的理解,对于一个在C下写代码比较多.偶尔会用到一些脚本语言写脚本的人来说,很是开阔眼界.<大话设计模式>的代码使用C#写成的 ...

  2. 自己动手实现智能家居之树莓派GPIO简介(Python版)

    [前言] 一个热爱技术的人一定向往有一个科技感十足的环境吧,那何不亲自实践一下属于技术人的座右铭:“技术改变世界”. 就让我们一步步动手搭建一个属于自己的“智能家居平台”吧(不要对这个名词抬杠啦,技术 ...

  3. [原创]上海好买基金招高级Java技术经理/运维主管/高级无线客户端开发等职位(内推)

    [原创]上海好买基金招高级Java技术经理/运维主管/高级无线客户端开发等职位(内推) 内部推荐职位 高级JAVA技术经理: 岗位职责: 负责项目管理(技术方向),按照产品开发流 ,带领研发团队,制定 ...

  4. 常见查找算法之php, js,python版

    常用算法 >>>1. 顺序查找, 也叫线性查找, 它从第一个记录开始, 挨个进行对比, 是最基本的查找技术 javaScript 版顺序查找算法: // 顺序查找(线性查找) 只做找 ...

  5. 豆瓣top250(go版以及python版)

      最近学习go,就找了一个例子练习[go语言爬虫]go语言爬取豆瓣电影top250,思路大概就是获取网页,然后根据页面元素,用正则表达式匹配电影名称.评分.评论人数.原文有个地方需要修改下patte ...

  6. 《UNIX环境高级编程(第3版)》

    <UNIX环境高级编程(第3版)> 基本信息 原书名:Advanced Programming in the UNIX Environment (3rd Edition) (Addison ...

  7. 数据结构之队列(Python 版)

    数据结构之队列(Python 版) 队列的特点:先进先出(FIFO) 使用链表技术实现 使用单链表技术,在表首尾两端分别加入指针,就很容易实现队列类. 使用顺序表list实现 # 队列类的实现 cla ...

  8. 数据结构之 栈 (Python 版)

    数据结构之 栈 (Python 版) -- 利用线性表实现栈 栈的特性: 后进先出 基于顺序表实现栈 class SStack(): ''' 基于顺序表 实现的 栈类 ''' def __init__ ...

  9. 【Python】《大话设计模式》Python版代码实现

    <大话设计模式>Python版代码实现 上一周把<大话设计模式>看完了,对面向对象技术有了新的理解,对于一个在C下写代码比较多.偶尔会用到一些脚本语言写脚本的人来说,很是开阔眼 ...

随机推荐

  1. SQL中的Null深入研究分析

    SQL中的Null深入研究分析 虽然熟练掌握SQL的人对于Null不会有什么疑问,但总结得很全的文章还是很难找,看到一篇英文版的, 感觉还不错. Tony Hoare 在1965年发明了 null 引 ...

  2. sencha touch xtype

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Android 如何处理崩溃的异常

    Android中处理崩溃异常    大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试 ...

  4. 别在细节上栽跟头------------mysql 字段类型详解

    也许你平时不在意,在设计数据库的时候,数字就设成int(10) 字符串就设成varchar(20)或者text 普通情况下是没有问题的,但是若不理解字段类型和长度的含义,总有一天你会在这里栽跟头, 这 ...

  5. dojo 六 使用query dojo/query

    要使用query,就要引入dojo/query包.query可以根据Dom里节点的标签名.id名.class名来检索一个或多个节点.---------------------------------- ...

  6. 《OD学HBase》20160821

    一.HBase性能调优 1. JVM内存调优 MemStore内存空间,设置合理大小 memstore.flush.size 刷写大小 134217728 = 128M memstore.mslab. ...

  7. HDU 1231 最大连续子序列

    和前面两道题一样 不过这题要求输出子序列首尾的元素的值,而且如果所有元素都小于0的话,规定子序列的和为0,并输出整个序列的首尾元素. //#define LOCAL #include <iost ...

  8. 【第三篇】说说javascript处理时间戳

    在做datagrid的时候,从数据库读出来的数据是/Date(1437705873240)/大概是这种形式,这个就是时间戳 我们需要把/Data和/去掉,才可以转成我们要的格式. 上代码 { fiel ...

  9. maven整合s2sh截图

  10. 约束优化方法之拉格朗日乘子法与KKT条件

    引言 本篇文章将详解带有约束条件的最优化问题,约束条件分为等式约束与不等式约束,对于等式约束的优化问题,可以直接应用拉格朗日乘子法去求取最优值:对于含有不等式约束的优化问题,可以转化为在满足 KKT ...