In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "Python regular expression has several functions", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "Python regular expression has several functions" this article.
Python regular expression is an expression that briefly expresses a set of strings. Regular expressions can easily check whether a string matches a certain pattern. There are six common functions in regular expressions: re.match, re.search, re.findall, re.compile, re.split, and re.sub.
The re.match () tch function matches from scratch, and if the starting position match is not successful, the matching result of the match function is none.
Re.search () the entire string and returns the first successful match
Re.findall () the entire string that returns a list (the most commonly used)
The re.compile () # compile function is used to compile a regular expression to generate a regular expression (Pattern) object
Re.split () # splits a string according to the result of regular expression matching and returns the list type
Re.sub () # replaces all the substrings that match the regular expression in a string and returns the replaced string
1. Re.match function
The match function matches from scratch, and if the starting position match is not successful, the matching result of the match function is none. If the match succeeds, the re.match method returns a matching object.
The syntax is as follows: re.match (pattern, string, flags=0)
Pattern: regular expressions that need to be matched
String: just match in that string
Flags: flag bit (default is 0), which controls how regular expressions are matched
Common flags is as follows:
Re.I ignores case when matching
Re.M multiline matching, affecting ^ and $
Re.S. The default does not match line breaks, so that. Match all characters, including line breaks
Re.U parses characters based on the Unicode character set. This flag affects\ w,\ W,\ b,\ B
Examples are as follows:
We can see that if the match function matches successfully, the re.match method returns a matching object instead of a matching regular expression; the matching position can be obtained through span ().
> import re
> astr='11you are 3344 my apple\ n11 pistachios, you\ n66a77'
> re.match
> re.match ('11 years journal astr). Span ()
(0,2)
> print (re.match ('you',astr))
None
2. Re.search function
Searches the entire string and returns the first successful match.
The syntax is as follows: re.search (pattern, string, flags=0)
Pattern: regular expressions that need to be matched
String: just match in that string
Flags: flag bit (default is 0), which controls how regular expressions are matched
Common flags is as follows:
Re.I ignores case when matching
Re.M multiline matching, affecting ^ and $
Re.S. The default does not match line breaks, so that. Match all characters, including line breaks
Re.U parses characters based on the Unicode character set. This flag affects\ w,\ W,\ b,\ B
Examples are as follows:
We can see that if the search function matches successfully, the re.search method returns a matching object instead of a matching regular expression; the matching position can be obtained through span (). If there is no match, it is returned as None.
> import re
> astr='11you are 3344 my apple\ n11 pistachios, you\ n66a77'
> re.search
> re.search ('you',astr)
> re.search ('you',astr). Span () # get the matching position through span ()
(2, 5)
> re.search ('11 years journal astr). Span ()
(0,2)
> print (re.search ('22 million dint ASTM))
None
3. Re.findall function
Search the entire string and return a list
Syntax is as follows: re.findall (string)
> import re
> astr='1you are 3344 my apple\ n11 pistachios, you\ n66a77'
>
['33,'44,'11,'66,'77]
> re.findall ('\ d {2Magne4}', astr) # list shows all the four numbers of 2muri, which matches greedily by default.
['3344,' 11,'66,'77]
> re.findall ('\ dflowers, last minute ASTM) # (1, infinity)
['1', '3344','11','66','77']
> re.findall ('\ dflowers, no matter how much) # (0, infinity)
['1', '3344,' 11,'66,'' '77 years,']
> re.findall ('\ dflowers match 1 / 0) # match 0 or 1
['1mm,' 3mm, '3mm,' 4pm, '4pm,' 1mm, '1mm,'' ',' 6percent, '6percent,', '7percent,' 7percent,'']
> re.findall ('\ d {2jue 3}?, astr) # A pattern followed by? Oh, not greedy matching, behind the range? Twice, just take it twice first.
['33,'44,'11,'66,'77]
>
['334', '6a7']
> re.findall ('^\ dharmjime) # begins with a number
['1']
> re.findall ('^\ dBell _ pr _ astro _ re.M) # multiline matching
['1mm,' 6']
> re.findall ('\ dflowers, girls, etc.) # ends with a number
['7']
> re.findall ('\ daddy match, which affects ^ and $
['7']
> re.findall ('\ d (.) (\ d)', astr,re.S) # is returned as a list, and each item is a tuple
[('3percent,' 4'), ('averse,' 7')]
4. Re.compile function
The compile function is used to compile a regular expression to generate a regular expression (Pattern) object.
Syntax is as follows: re.compile (pattern,flags=0)
Pattern: regular expressions that need to be matched
Flags: flag bit (default is 0), which controls how regular expressions are matched
Common flags is as follows:
Re.I ignores case when matching
Re.M multiline matching, affecting ^ and $
Re.S. The default does not match line breaks, so that. Match all characters, including line breaks
Re.U parses characters based on the Unicode character set. This flag affects\ w,\ W,\ b,\ B
Examples are as follows:
> import re
> astr='AS12as34er567q! "3456'
> m1=re.compile (r'\ d\ d') # compile
> m1.search (astr). Group () # match
'12'
> m1.findall (astr)
['12,'34,'56,'34,'56]
>
> > m2.findall (astr) # match
['Abigail,' a']
5. Re.split function
Splits a string according to the result of a regular expression match and returns the list type.
Syntax is as follows: re.split (pattern, string,? maxsplit=0?,flags=0)
Pattern: regular expressions that need to be matched
String: just match in that string
Maxsplit: number of separations. Maxsplit=1 separates once. Default is 0. There is no limit on the number of separations.
Flags: flag bit (default is 0), which controls how regular expressions are matched.
Common flags is as follows:
Re.I ignores case when matching
Re.M multiline matching, affecting ^ and $
Re.S. The default does not match line breaks, so that. Match all characters, including line breaks
Re.U parses characters based on the Unicode character set. This flag affects\ w,\ W,\ b,\ B
Examples are as follows:
> import re
> astr='AS12as34er567q! "3456'
> astr.split ('12') # split by 12
['AS',' as34er567q! "3456']
> re.split ("\ d {2}", astr) # split by two numbers
['AS',' as', 'er',' 7q! ",',']
> re.split ("\ d +", astr) # split by numbers
['AS',' as', 'er',' Q! ",']
>
> m3.split (astr)
['AS',' as', 'er',' Q! ",']
> m3.split (astr,3) # specify how many times to split
['AS',' as', 'er',' Q! "3456']
6. Re.sub function
Replaces all substrings that match the regular expression in a string and returns the replaced string.
The syntax is as follows: re.sub (pattern, repl, string, count=0,flags=0)
Pattern: regular expressions that need to be matched
Repl: the replacement string, which can also be a function.
String: just match in that string
Count: the maximum number of substitutions after a pattern match. The default of 0 means replacing all matches.
Flags: flag bit (default is 0), which controls how regular expressions are matched
Common flags is as follows:
Re.I ignores case when matching
Re.M multiline matching, affecting ^ and $
Re.S. The default does not match line breaks, so that. Match all characters, including line breaks
Re.U parses characters based on the Unicode character set. This flag affects\ w,\ W,\ b,\ B
Examples are as follows:
> import re
> astr='AS12as34er567q! "3456'
> re.sub ("5", '9century centr) # replace 5 with 9
'AS12as34er967q!' 3496'
> m4=re.compile (r "\ d +")
> m4.sub ('', astr) # replace the number with an empty string
'AS as er q!'
> m4.sub ('', astr,2) # specifies how many times to replace
'AS as er567q! '3456
That's all of the article "there are several functions for Python regular expressions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.