High-level usage of Python regular expressions

Source: Internet
Author: User
For Python, learning is a way to learn how to use the module RE. This article will show you some of the advanced techniques that everyone should know.

Compiling the Regular object

The Re.compile function generates a regular expression object based on a pattern string and an optional flag parameter. The object has a series of methods for regular expression matching and substitution. There is a slight difference in usage, for example, matching a string can be used in the following way:

If you use compile, you will become:

Why do you use it that way? In fact, in order to improve the speed of regular matching, reuse regular expression object. Let's compare the efficiency of 2 different ways:

You can see that the second way is much faster. In the actual work you will find that the more you use the compiled regular expression object, the better the effect will be.

Group (Group)

You may have seen the use of grouping matching content:

By adding parentheses to the object to be matched, you can precisely correspond to the results of the match. We can also do nested groupings:

The groupings can meet the requirements, but sometimes the readability is poor, and the grouping can be named:

The readability is now very high.

String match

Students who have studied SED may have seen the following alternate usage:

This \1 represents the result of the preceding regular match. The above SED is also the match to the result of the addition of brackets.

This usage also exists in the RE module:

It is also possible to use named groupings:

Nearby matches (look around)

The re module also supports nearby matches to see the examples to understand:

Use functions when regular matches are used

Most of what we've seen before is a match for an expression, but sometimes the requirements are much more complex, especially when it comes to substitution.

For example, through the slack API can get chat records, such as the following sentence:

where < @U1EAT8MG9 > and < @U0K1MF23Z > are 2 real users, but are encapsulated by slack and need to get this correspondence through other interfaces,

The result is something like this:

After parsing the correspondence, I also hope that the angle brackets are removed, and the result of the substitution is "@xiaoming, @laolin well, it's true."

How do you do it with regular?

So pattern can be a function, of course.

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.