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.

     
  • 相关阅读:
    MT【347】单变量求最值
    MT【346】拐点处分界
    MT【345】三个绝对值的和
    MT【344】构造函数
    MT【343】三数平方法
    MT【342】条件为非负实数
    MT【341】换元变形
    MT【340】彭塞列闭合定理
    MT【339】待定系数
    MT【338】分式变形
  • 原文地址:https://www.cnblogs.com/webRobot/p/5224147.html
Copyright © 2011-2022 走看看