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 use Python regular expressions

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use Python regular expressions, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Python3 regular expression

A regular expression is a special sequence of characters that helps you easily check whether a string matches a pattern. This article mainly describes the main functions in the re package.

Before we elaborate on the functions in the re package, let's first look at the pattern of motion regular expressions, even if a special syntax is used to represent a regular expression.

1.match function

Re.match attempts to match a pattern from the beginning of the string, and match () returns none if the match is not successful.

Function usage: re.match (pattern, string, flags=0)

Pattern: regular expression to match string: string flags: flag bit to match, which is used to control how regular expressions are matched, such as case sensitivity, multiline matching, and so on.

Re.I ignores case

Re.L indicates that the special character set\ w,\ W,\ b,\ B,\ s,\ S depends on the current environment

Re.M multiline mode re.S is'. 'and any character including a newline character ('. 'excluding newline characters)

Re.U represents the special character set\ w,\ W,\ b,\ B,\ d,\ D,\ s,\ S depending on the Unicode character attribute database

Re.X ignores spaces and comments after'#'to increase readability

Match object method:

Group (num=0): a string that matches the entire expression, and group () can enter more than one group number at a time, in which case it returns a tuple containing the values corresponding to those groups.

Groups (): returns a tuple containing all the team strings, from 1 to the contained group number.

Import reprint (re.match ("xixi", "xixi__heihei"). Group () xixiline = 'Cats are smarter than dogs'b = re.match (r' (. *) are (. *?). *', line, re.M | re.I) print (b.group ()) # returns all print (b.group (1)) # returns the first group, that is, print (b.group (2)) # corresponding to (. *) returns the second group, that is, (. *?) Corresponding Cats are smarter than dogsCatssmarter2.search function

Re.search scans the entire string and returns the first successful match.

Function usage: re.search (pattern, string, flags=0)

Print (re.match ('heihei',' xixi__heihei')) print (re.search ('heihei',' xixi__heihei'). Group () Noneheiheiline = 'Cats are smarter than dogs'b = re.search (r' (. *) are (. *?). *', line, re.M | re.I) print (b.group ()) # returns all print (b.group (1)) # returns the first group That is, the print (b.group (2)) # corresponding to (. *) returns the second group, namely (. *?) The difference between the corresponding Cats are smarter than dogsCatssmartersearch and match

Match only matches the beginning of the string. If the string does not match the regular expression at the beginning, the match fails, and the function returns None; and search matches the entire string until a match is found.

3. Sub function

Re provides re.sub to replace matches in strings.

Function usage: re.sub (pattern, repl, string, count=0, flags=0)

Pattern: the pattern string in the regular.

Repl: the replacement string, which can also be a function.

String: the original string to be found and replaced.

Count: the maximum number of substitutions after a pattern match. The default of 0 means replacing all matches.

Flags: the matching pattern used at compilation time, in digital form.

Phone = '133133-3333-3333 # this is a phone number'num = re.sub (rattlestick,', phone) print ('phone num', num) # remove the comment and find the one that starts with #. Num = re.sub (r'\ digital,'', phone) print ('phone num', num) # unless the digital content phone num 133-3333-3333 phone num 13333333333

Case where repl is a function

Def double (matched): value = int (matched.group ('value')) return str (value * 2) s =' A233Sfd34'print (re.sub ('? P\ d +)', double, s)) A466Sfd684.compile function

The compile function is used to compile regular expressions and generate a regular expression (Pattern) object for use by the match () and search () functions.

Function use: re.compile (pattern, flags)

Pattern = re.compile (rattlespace dudes') m = pattern.match ('ones123412') print (m) None5.findall

Finds all the substrings matched by the regular expression in the string and returns a list, or an empty list if no matches are found.

Note: match and search match once, but findall matches all.

Function use: findall (string, pos, endpos)

String the string to be matched.

The optional parameter pos, which specifies the starting position of the string, defaults to 0.

Endpos optional parameter that specifies the end position of the string, which defaults to the length of the string.

Pattern = re.compile (r'\ dbath') result1 = pattern.findall ('xixixix 123 heihiehei 456') result2 = pattern.findall (' xixixix 123 heihiehei 456mm, 0,15) print (result1) print (result2) ['123', '456'] ['123'] 6.finditer

Similar to findall, all the substrings matched by the regular expression are found in the string and returned as an iterator.

Ittt = re.finditer (r'\ dfasdf123asdf534') for ttt in ittt: print (ttt.group ()) 121235347.split

The split method splits the string according to the substring that can be matched and returns the list.

Function uses:

Re.split (pattern, string, maxsplit=0, flags=0)

Pattern: matching regular expressions

String: the string to match.

Maxsplit: number of separations. Maxsplit=1 separates once. Default is 0. There is no limit on the number of separations.

Flags: flag bit, which controls how regular expressions are matched

Print (re.split ('\ Warehouse, 'xxixix, xixixi, hehiehei')) print (re.split (' (\ W+)', 'xxixix, xixixi, hehiehei')) [' xxixix', 'xixixi',' hehiehei'] [',', 'xxixix',' xixixi', 'hehiehei'] Thank you for reading this article carefully I hope the article "how to use Python regular expressions" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Development

Wechat

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

12
Report