Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Python re.sub reverse reference

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you how to achieve the relevant knowledge of Python re.sub reverse reference, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

The re module is a module provided in the Python standard library for dealing with regular expressions. Using the re module, you can easily use regular expressions to realize matching, substitution and other operations in strings.

Match grouping

The match function is provided in the Python re module to match the regular expression rules specified in the string. For example, if you want to match the Isaac Newton in "Isaac Newton, physicist", you can use the regular expression\ w +\ w +, which runs as follows:

> m = re.match ("\ w +\ w +", "Isaac Newton, physicist") > m

The first argument to re.match is the specified regular expression rule, and the second argument is the string to be matched. The regular expression rule\ w + matches a continuous segment of characters, requiring that the number of characters matched is greater than 1. \ w +\ w + means that two consecutive characters are matched and separated by a space.

When performing a match using match, the matching results are grouped. You can view the grouping results through the group () API returned by match.

> m.group (0) 'Isaac Newton'

By default, match produces only one packet, the 0th, which represents the entire matched content. For the above example, the 0th grouping is the complete content that\ w +\ w + matches, that is, Issac Newton.

Use parentheses () in regular expressions to manually specify matching groups. For example, if you want to group Issac and Newton as two groups, you can change the regular expression to (\ w +) (\ w +):

> m = re.match ("(\ w +) (\ w +)", "Isaac Newton, physicist")

In the regular expression (\ w +) (\ w +), two groups are specified. The matching content of the two groups is\ w +, and the two groups are separated by spaces.

Use groups () to view all the groups in the match result:

> m.groups () ('Isaac',' Newton')

You can also view each packet individually using the group () interface, where group (0) still represents the complete match result, group (1) represents the first packet in the match result, group (2) represents the second packet, and so on:

> m.group (0) 'Isaac Newton' > m.group (1)' Isaac' > m.group (2) 'Newton're.sub matching and replacement

The re.match () function provides a matching interface for regular expressions. Re.sub () can not only match regular expressions, but also replace the results in a string to generate a new string.

For example, to replace the result matched by (\ w +) (\ w +) in a string with Albert Einstein, you can write:

> re.sub ("(\ w +) (\ w +)", "Albert Einstein", "Isaac Newton, physicist") 'Albert Einstein, physicist'

In re.sub, the first parameter represents the matching regular expression, the second parameter represents the replacement expression, and the third parameter represents the original string.

The replacement expression here is the new string Albert Einstein specified manually, which has nothing to do with the contents of the original string. If you want to reuse the contents of the original string, you need to use re.sub 's back-referencing feature.

Reverse reference

A backreference means that a match to the content in the original string can be referenced during the process of specifying the replacement result. For example, (\ w +) (\ w +) matches Isaac Newton in the original string and rewrites the result to FirstName: Isaac, LastName: Newton using the matching result.

Since a reference is needed, there must be an expression that can represent the matching content. It just so happens that the matching result of re.sub has the same grouping as re.match, so you only need to reference the result of the grouping in the replacement expression. There are several ways to quote:

\ number: for example,\ 1 indicates the first grouping in the matching result, that is, the Isaac part of the example.

\ g: for example,\ g, like the\ number representation, also represents the first grouping in the matching result. Compared with the\ number representation,\ g avoids ambiguity. Just imagine, if you want to use\ number to replace the Isaac matched to the first packet with Isaac0, then you need to use\ 10, which means to add 0 after the first packet, but the program will recognize it as the 10th packet. The use of\ g only needs to be written as\ G0.

Going back to the starting example, rewrite the matching result Isaac Newton to FirstName: Isaac, LastName: Newton, which can be implemented with the following expression:

> re.sub ("(\ w +) (\ w +)", "FirstName:\ g, LastName:\ g", "Isaac Newton, physicist") 'FirstName: Isaac, LastName: Newton, physicist' above are all the contents of the article "how to implement Python re.sub backreferences". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report