In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what the regular expression processing function commonly used in Python is. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
A regular expression is a special sequence of characters, which is used to succinctly express a set of string features and to check whether a string matches a certain pattern.
In Python, we use the re module by calling the re library:
Import re
For more information on regular expression syntax patterns and operators, see https://www.runoob.com/python/python-reg-expressions.html#flags
The regular expression processing functions commonly used in Python are described below.
Re.match function
The re.match function matches the regular expression from the starting position of the string, returns the match object, and match () returns None if the starting position match is not successful.
Re.match (pattern, string, flags=0)
Pattern: the matching regular expression.
String: the string to match.
Flags: flag bit that controls how regular expressions are matched, such as case sensitivity, multiline matching, and so on. The specific parameters are:
Re.I: ignore 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: namely. And any character, including a newline character (. Newline characters are not included.
Re.U: indicates that the special character set\ w,\ W,\ b,\ B,\ d,\ D,\ s,\ S depends on the Unicode character attribute database.
Re.X: to increase readability, ignore spaces and comments followed by #.
Import re# matches r1=re.match ('abc','abcdefghi') print (R1) # does not match r2=re.match (' def','abcdefghi') print (R2) from start position
Running result:
Where span represents the index of the entire substring that matches successfully.
Use the group (num) or groups () match object function to get the matching expression.
Group (num): a string that matches the entire expression. Group () can enter more than one group number at a time, and it will return 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 res='This is a demo'r1=re.match (r'(. *) is (. *), s) r2=re.match (r'(. *) is (. *), s) print (r1.group ()) print (r1.group (1)) print (r1.group (2) print (r1.groups ()) print () print (r2.group ()) print (r2.group (1)) print (r2.group (2)) print (r2.groups ())
Running result:
(. *) and (. *?) in the above code Represents greedy and non-greedy matching of regular expressions, as detailed here: https://www.jb51.net/article/31491.htm
Re.search function
The re.search function scans the entire string and returns the first successful match, and if the match succeeds, it returns the match object, otherwise it returns None.
Re.search (pattern, string, flags=0)
Pattern: the matching regular expression.
String: the string to match.
Flags: flag bit that controls how regular expressions are matched, such as case sensitivity, multiline matching, and so on.
Import re# matches r1=re.search ('abc','abcdefghi') print (R1) # does not match r2=re.search (' def','abcdefghi') print (R2) from start position
Running result:
Use the group (num) or groups () match object function to get the matching expression.
Group (num=0): a string that matches the entire expression. Group () can enter more than one group number at a time, and it will return 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 res='This is a demo'r1=re.search (r'(. *) is (. *), s) r2=re.search (r'(. *) is (. *), s) print (r1.group ()) print (r1.group (1)) print (r1.group (2) print (r1.groups ()) print () print (r2.group ()) print (r2.group (1)) print (r2.group (2)) print (r2.groups ())
Running result:
From the above, it is not difficult to see the difference between re.match and re.search: re.match only matches the starting position of the string, as long as the starting position does not match the regular expression, the match fails, while re.search matches the entire string until a match is found.
Re.compile function
The compile function is used to compile regular expressions and generate a regular expression object for use by the match () and search () functions.
Re.compile (pattern [, flags])
Pattern: a regular expression in the form of a string
Flags: optional, indicating matching patterns, such as ignoring case, multiline patterns, etc.
Import re# matching digits r=re.compile (r'\ dudes') r1=r.match ('This is a demo') r2=r.match (' This is 111 and That is 222) print (R3) r3=r.match ('This is 111 and That is 222)
Running result:
Findall function
Searches for a string and returns all substrings matched by the regular expression as a list, or an empty list if no matches are found.
It is important to note that match and search match once, while findall matches all.
Findall (string [, pos [, endpos]])
String: the string to match.
Pos: optional parameter that specifies the starting position of the string. The default is 0.
Endpos: optional parameter that specifies the end position of the string, which defaults to the length of the string.
Import re# matching digits r=re.compile (r'\ daddy') r1=r.findall ('This is a demo') r2=r.findall (' This is 111 and That is 222) print (R3) r3=r.findall ('This is 111 and That is 222) print (R3)
Running result:
Re.finditer function
Similar to findall, all the substrings matched by the regular expression are found in the string and returned as an iterator.
Re.finditer (pattern, string, flags=0)
Pattern: the matching regular expression.
String: the string to match.
Flags: flag bit that controls how regular expressions are matched, such as case sensitivity, multiline matching, and so on.
Import re r=re.finditer (for i in r: print (i.group ()). This is 111 and That is 222')
Running result:
Re.split function
A string is segmented according to a substring matched by a regular expression and returned as a list.
Re.split (pattern, string [, maxsplit=0, flags=0])
Pattern: the matching regular expression.
String: the string to match.
Maxsplit: split times, maxsplit=1 split once. Default is 0, unlimited number of times.
Flags: flag bit that controls how regular expressions are matched, such as case sensitivity, multiline matching, and so on.
Import re r1=re.split ('\ Warriors') r2=re.split ('\ is' and That is 222') r3=re.split ('\ is' and That is 222') r4=re.split ('\ dice 'r4=re.split) print (R1) print (R2) print (R2) print (R4)
Running result:
Re.sub function
The re.sub function is used to replace matches in a string.
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.
Import re r='This is 111Numeric r1=re.sub (print (R1) # Delete the non-numeric string r2=re.sub (r'\ DPRI) print (R2) delete the numeric string from the string.
Running result:
This is the end of this article on "what is the regular expression processing function commonly used in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
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.