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

Example Analysis of regular expression re.sub & re.subn in python

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

Share

Shulou(Shulou.com)06/02 Report--

This article is about analyzing examples of regular expressions re.sub & re.subn in python. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Python regular expression module introduction

Python has added the re module since version 1.5, which provides Perl-style regular expression patterns. Python versions prior to 1.5 provided Emacs-style patterns through the regex module. Emacs style patterns are less readable and less functional, so try not to use regex modules when writing new code, although occasionally you may find them in older code.

By its very nature, regular expressions (or RE) are a small, highly specialized programming language that (in Python) is embedded in Python and implemented through the re module. With this little language, you can specify rules for the corresponding string set you want to match; that string set might contain English sentences, e-mail addresses, TeX commands, or whatever you want to fix. Then you can ask questions like "Does this string match the pattern? Does "or" match the pattern in any part of this string? "。You can also use RE to modify or split strings in various ways.

Regular expression patterns are compiled into a series of bytecodes that are then executed by a matching engine written in C. In advanced usage, you might also want to pay close attention to how the engine executes a given RE, and how the RE is written in a particular way to make the bytecode produced run faster. This article doesn't cover optimization, because that requires you to have a good grasp of the internals of the matching engine.

The regular expression language is relatively small and limited (limited in functionality), so not all string processing can be done with regular expressions. Of course, there are some tasks that can be done with regular expressions, but the final expression can be extremely complex. In these cases, it may be better to write Python code to handle them; although Python code is slower than a neat regular expression, it is easier to understand.

A common use of regular expressions is to find all strings that match patterns and replace them with different strings. The sub method provides a replacement value, either a string or a function, and a string to be processed.

Grammar:

re.sub(pattern, repl, string[, count])

Replace each matching substring in string with repl and return the replaced string.

When repl is a string, you can use\id or\g,\g to refer to the grouping, but you cannot use the number 0.

When repl is a method, the method should accept only one argument (Match object) and return a string for substitution (grouping cannot be referenced in the returned string).

Count is used to specify the maximum number of substitutions, if not specified, replace all.

re.subn(pattern, repl, string[, count])

Returns (sub(repl, string[, count]), number of substitutions).

Case:

#coding=utf-8import restr = "https://i.cnb1logs.co2m/Edi3tPosts.asp4x? opt=999"pattern=re.compile(r'(\.) ')print '\. :' ,re.sub(pattern,'-', str)pattern=re.compile(r'\/([^*]+)\/') print '\/([^*]+)\/ :' ,re.sub(pattern,r'\1', str)pattern = re.compile(r'(\w +)(\d+)')#slice test print re.split(pattern,str)print re.sub(pattern,r'\3\1', str)#subn count sub substitution times print re.subn(pattern,r'\3\1', str)

Output

\. : https://i-cnb1logs-co2m/Edi3tPosts-asp4x? opt=999\/([^*]+)\/ : https:/i.cnb1logs.co2mEdi3tPosts.asp4x? opt=999['https://i. ', 'cn', 'b', '1', 'logs. ', 'c', 'o', '2', 'm/', 'Ed', 'i', '3', 'tPosts. ', 'as', 'p', '4', 'x? opt=', '9', '9', '9', '']https://i.1 cnlogs.2 cm/3 EdtPosts.4 asx? opt=9 9('https://i.1 cnlogs.2 cm/3 EdtPosts.4 asx? opt=9 9', 5)***Repl Closed*** Thank you for reading! About "python regular expression re.sub & re.subn sample analysis" This article is shared here, I hope the above content can be of some help to everyone, so that we can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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