Python loose Regular Expression Usage Analysis, python Regular Expression

Source: Internet
Author: User

Python loose Regular Expression Usage Analysis, python Regular Expression

This example describes the usage of Python loose regular expressions. We will share this with you for your reference. The details are as follows:

Python allows you to use the so-called loose regular expression to complete this task. A loose Regular Expression differs from a compact Regular Expression in two aspects:

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

2. Ignore comments. Comments in a loose regular expression are the same as those in common Python code: Start With A # symbol and end with the end of a line. In this case, comment in a multi-line string instead of in the source code, they work in the same way.

The following is an example of a loose regular expression. intuitively, the regular expression mode is divided into several lines for writing. We can add our annotations to each line. In this way, we can quickly see the role of this regular expression and 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), 400 (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), 40 (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 of 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 using a loose regular expression, you must pass another parameter re. VERBOSE, which 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 of a loose type or a compact type. Therefore, it must explicitly indicate that a regular expression is of a loose type. So

Re. search (pattern, 'M', re. VERBOSE) # loose Regular Expression

Follow:

Re. search (pattern, 'M') # The default is a "Compact" regular expression.

The results are different.

Below are some common Regular Expressions:

^ Matches the start of a string.
$ Matches the end of a string.
\ B matches the boundary of a word.
\ D matches any number.
\ D matches any non-numeric characters.
X? Match an Optional x character (in other words, it matches 1 or 0 x characters ).
X * matches 0 or multiple times x characters.
X + matches 1 or multiple x characters.
X {n, m} matches x characters, at least n times, at most m times.
(A | B | c) either matches a, B, or c.
(X) indicates a remembered group. We can use the re. search function to return the value of the object's groups () function.

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.