zoukankan      html  css  js  c++  java
  • 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.

     
  • 相关阅读:
    MxNet Windows下安装
    Binary Tree Postorder Traversal--leetcode难题讲解系列
    Populating Next Right Pointers in Each Node II--leetcode难题讲解系列
    Recover Binary Search Tree--leetcode难题讲解
    bash + script
    Linux笔记
    谷歌面经 Tree Serialization
    Python strange questions list
    bit操作 转
    ubuntu系统从中文环境改成英文环境
  • 原文地址:https://www.cnblogs.com/webRobot/p/5224147.html
Copyright © 2011-2022 走看看