Python loosely-Regular expression usage analysis

Source: Internet
Author: User
This example describes the Python loose regular expression usage. Share to everyone for your reference, as follows:

Python allows users to accomplish this task with the so-called loosely regular expression. The main difference between a loose regular expression and a compact regular expression is shown in two ways:

1. Ignore whitespace characters . Space characters, tabs, and carriage returns do not match themselves, they do not participate in the match at all. (If you want to match a space character in a loose regular expression, you must add a backslash before it to escape it)

2. Ignore annotations . Comments in loose regular expressions are the same as in normal Python code: starts with a # symbol and ends at the end of the line. In this case, comments are taken in a multiline string instead of in the source code, and they work in the same way.

Here is an example of a loose regular expression, and intuitively, the regular expression pattern is written in several lines, and we can match each line with our annotations. So after a while to look back, we can quickly know the role of this regular expression, enhance the readability of the code.

>>> Import re>>> pattern = "" "  ^          # Beginning of string  m{0,4}       # thousands-0 to 4 M ' s
   (cm| cd| D? c{0,3})  # hundreds-900 (CM), (CD), 0-300 (0 to 3 C's),            #      or 500-800 (D, followed by 0 to 3 C ' s)  (xc| xl| L? x{0,3})  # tens-90 (XC), (XL), 0-30 (0 to 3 x's),            #    or 50-80 (L, followed by 0 to 3 X ' s)  (ix|iv| V? i{0,3})  # ones-9 (IX), 4 (IV), 0-3 (0 to 3 I's),            #    or 5-8 (V, followed by 0 to 3 I ' s)  $          # end O F string  "" ">>> re.search (Pattern, ' M ', re. VERBOSE) <_sre. Sre_match object at 0x01401570>>>> re.search (pattern, ' mcmlxxxix ', re. VERBOSE) <_sre. Sre_match object at 0x014015c0>>>> re.search (pattern, ' M ') >>>

When you use a loose regular expression, you must pass another parameter, re. VERBOSE, this parameter is a constant defined in the RE module, marking that the regular expression to be matched is a loose regular expression. Python cannot automatically detect whether a regular expression is loosely typed or compact, so it must explicitly indicate that a regular expression is loosely typed. So

Re.search (Pattern, ' M ', re. VERBOSE) #松散正则表达式

With:

Re.search (Pattern, ' M ')) #默认为 "compact" regular expression

The results are not the same.

The following are some of the most common regex expressions :

^ matches the start of the string.
$ matches the end of the string.
\b matches the boundary of a word.
\d matches any number.
\d matches any non-numeric character.
X? Matches an optional x character (in other words, it matches 1 or 0 x characters).
x* matches 0 or more x characters.
x+ matches 1 or more x characters.
X{N,M} matches x characters, at least n times, up to M times.
(a|b|c) either match a, or match B, or match C.
(x) typically represents a memory group (remembered group). We can use the Re.search function to return the object's groups () function to get its value.

Read more about Python topics: python Regular expression Usage summary, Python data structure and algorithm tutorials, Python socket Programming Tips Summary, Python function Usage tips summary, "How-to" Python string manipulation Tips Summary, Python Introductory and Advanced classic tutorials, and Python file and directory operations tips

I hope this article is helpful for Python program design.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.