In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "python regular expression related knowledge what", the content is easy to understand, clear, hope to help you solve doubts, the following let Xiaobian lead you to study and learn "python regular expression related knowledge what are" this article.
Regular expression module
Python has a built-in package called re, which can be used to process regular expressions. Import the re module:
Regular expressions in import rePython
After you import the re module, you can start using regular expressions.
For example: search for a string to see if it begins with "The" and ends with "Spain":
Import retxt = "The rain in Spain" x = re.search ("^ The.*Spain$", txt) if x: print ("match succeeded!") else: print ("match failed")
Run:
Of course, you do not understand this example now, since hand-in-hand teaching will not teach everyone to rise to the top in one step.
Regular expression function findall () function
The findall () function returns a list of all matches.
For example: print a list of all matches
Import retxt = "Sichuan rookie" x = re.findall ("rookie", txt) print (x)
Run and return:
The list contains matches in the order in which they are found. If no match is found, an empty list is returned:
Import retxt = "rookie is not good" x = re.findall ("Chuan Chuan", txt) print (x) if (x): print ("match is successful") else: print ("can't find this!")
Run and return:
Search () function
The search () function searches for matches in the string and returns a Match object if there is a match. If there is more than one match, only the first occurrence of the match is returned.
For example: search for the first white space character in a string:
Import retxt = "rookie" x = re.search ("\ s", txt) print ("the first space character is in position:", x.start ())
Running result:
If no match is found, None returns the value:
Import retxt = "what flies in the sky is a rookie" x = re.search ("Sichuan", txt) print (x)
Return:
Split () function
The split () function returns a list in which the string is split each time it matches.
For example: split at each white space character
Import retxt = "rookie learning python" x = re.split ("\ s", txt) print (x)
Run and return:
You can control the number of occurrences by specifying the maxsplit parameter
For example: split the string only when it first appears:
Import re#Split the string at the first white-space character:txt = "flying rookies" x = re.split ("\ s", txt, 1) print (x)
Return:
Sub () function
The sub () function replaces the match with the text of your choice.
For example, replace it with only
Import retxt = "learn python to find Sichuan rookie" x = re.sub ("just", "only", txt) print (x)
Run:
You can control the number of replacements by specifying the count parameter:
For example, the first two replacements appear:
Import retxt = "learn python on Sichuan rookie" x = re.sub ("just", "only", txt,2) print (x)
Return:
Metacharacter list symbol
[] for a set of characters
For example: # finds all lowercase characters between "a" and "m" in alphabetical order
Import retxt = "apple chuanchuan" # find all lowercase characters x = re.findall ("[Amurm]", txt) print (x) between "a" and "m" in alphabetical order
Run:
Escape character
* * represents a special sequence (can also be used to escape special characters)
For example, match all numbers:
Import retxt = "I'm 20 years old" # find all numeric characters x = re.findall ("\ d", txt) print (x)
Run and return:
Arbitrary symbol
. You can have any character (except newline characters).
For example: search for sequences that begin with "he", followed by two (arbitrary) characters and an "o"
Import retxt = "hello world" # search for sequence x = re.findall ("he..o", txt) print (x) starting with "he" followed by two (any) characters and an "o"
Run and return:
Start character
The ^ symbol is used to start the match.
Import retxt = "Sichuan rookie is flying" x = re.findall ("^ Chuan", txt) if x: print ("Wow, I got a match") else: print ("Oh, no match")
Run:
Terminator
The $symbol is used to match the end, for example: whether the match string ends with "world"
Import retxt = "hello world" # whether the match string ends with "world" x = re.findall ("world$", txt) if x: print ("match is successful") else: print ("match does not match")
Run:
Asterisk symbol
The asterisk is used to match zero or more occurrences.
Import retxt = "A rookie flying in the sky, learn python to find a Sichuan rookie!" # check whether the string contains "ai" followed by 0 or more "x" characters: X = re.findall ("rookie *", txt) print (x) if x: print ("match!") else: print ("so angry, it doesn't match")
Run:
Plus sign
+ used to match one or more occurrences
For example, check whether the string contains "rookie" followed by one or more "rookie" characters:
Import retxt = "fly, rookies!" # check whether the string contains "rookie" followed by one or more "rookie" characters: X = re.findall ("rookie +", txt) print (x) if x: print ("match!") else: print ("bored, no match")
Run:
Collection symbol
{} the exact number of occurrences specified
For example: check whether the string contains "Chuan"
Import retxt = "Sichuan rookie is not good!" # check whether the string contains the words "Sichuan" x = re.findall ("Sichuan {2}", txt) print (x) if x: print ("matched twice") else: print ("No match, handsome")
Return:
Or character
| | matches either of the two |
For example: match string rookie or me.
Import retxt = "have the rookies learned python? the string is also a rookie!" x = re.findall ("rookie | it's me", txt) print (x) if x: print ("match!") Else: print (match failed)
Run:
Special sequence specified character
\ A: returns a match if the specified character is at the beginning of the string.
For example: match characters that begin with vegetable characters
Import retxt = "Rookie is here" x = re.findall ("\ A dish", txt) print (x) if x: print ("Yes match") else: print ("match not available")
Run:
Specify the beginning and end
\ b returns a match with the specified character at the beginning or end of the word (the beginning of the "r" ensures that the string is treated as the original string).
For example: the beginning of matching love
Import retxt = "Love you, Chuan Chuan" x = re.findall (r "\ b love", txt) print (x) if x: print ("match") else: print ("match not")
Run:
Another example: match the end of Sichuan
Import retxt = "Love you, Sichuan" x = re.findall (r "Chuan\ b", txt) print (x) if x: print ("match") else: print ("match not")
Run:
Match intermediate characters
\ B returns a match that exists the specified character but is not at the beginning (or end) of the word (the "r" at the beginning ensures that the string is treated as the "original string")
For example, I match a rookie:
Import retxt = "I'm a rookie" # check for the existence of "ain", but not at the beginning of the word: X = re.findall (r "\ rookie", txt) print (x) if x: print ("match!") else: print ("not a match!")
Run:
But the end of your match will return empty, for example, I match the bird:
Import retxt = "Sichuan rookie" # check for "bird", but not at the end of the word: X = re.findall (r "bird\ B", txt) print (x) if x: print ("match") else: print ("not found")
Run:
Matching number
\ d returns a match of a string containing a number (a number between 0 and 9).
For example:
Import retxt = "I'm 20 years old" # check whether the string contains any digits (0-9 digits) x = re.findall ("\ d", txt) print (x) if x: print ("Whoa, it matches the number") else: print ("not found")
Run:
Match non-numeric
\ d returns a match with a string that does not contain a number
For example:
Import retxt = "I'm 20 years old" # matches any non-numeric symbol x = re.findall ("\ D", txt) print (x) if x: print ("match, happy!") else: print ("mismatch, angry")
Run:
Space matching
\ s returns a match string containing a match of white space characters.
For example:
Import retxt = "I am a Sichuan rookie" # matches any space character x = re.findall ("\ s", txt) print (x) if x: print ("match") else: print ("match does not match")
Run:
Match non-space
\ s returns a match of a string that does not contain space characters
Import retxt = "Rookie is me" # matches any non-empty character x = re.findall ("\ S", txt) print (x) if x: print ("match!") else: print ("match does not match")
Run:
Match any number and letter
Returns a match in which the string contains any word character (character from a to Z, number from 0 to 9, and underscore _ character)
For example:
Import retxt = "Rookie is a string" # returns a match in each word character (from a to z, a number from 0 to 9, and an underscore _ character): X = re.findall ("\ w", txt) print (x) if x: print ("match") else: print ("match not wow")
Run:
Match any non-numeric and alphabetic
Returns a string that does not contain a match of any word characters, and returns a match (not between An and Z) in each non-word character. "!" "?" Blank space, etc.)
For example:
Import retxt = "is it me, rookie? I don't believe it!" # returns a match (not between An and Z) in each non-word character. "!" "?" Blank bits, etc.): X = re.findall ("\ W", txt) print (x) if x: print ("match!") else: print ("match does not match")
Run:
Match the ending
\ Z returns a match if the specified character is at the end of the string.
For example:
Import retxt = "Sichuan is a rookie" x = re.findall ("Ah\ Z", txt) print (x) if x: print ("match!") else: print ("match not") collection set specifier range match
For example, collection: [arn]
Import retxt = "The rain in Spain" x = re.findall ("[arn]", txt) print (x) if x: print ("match!") else: print ("match not") matches lowercase letters in any range
Returns a match for any lowercase character, alphabetically between an and n.
For example:
Import retxt = "hello wo r l d" x = re.findall ("[a Murn]", txt) print (x) if x: print ("match!") else: print ("match not available")
Run:
By the same token, the other cases in turn are as follows:
[^ arn] returns a match for any character except a, r, and n
[0123] returns a match for any specified number (0, 1, 2, or 3)
[0-9] returns the match of any number between 0 and 9
[0-5] [0-9] returns the match of any two digits from 00 to 59
[a-zA-Z] returns any character match between an and z in alphabetical order, lowercase or uppercase
[+] in the collection, +, *,., |, (), $, {} has no special meaning, so [+] means: returns a match of any + character in the string. Let me give you an example:
Import retxt = "5 matches 6 11" # check whether the string has any + characters: X = re.findall ("[+]", txt) print (x) if x: print ("match") else: print ("match not")
Run:
Matching object
Matching objects are objects that contain information about searches and results. Note: if there is no match, None will return the value instead of the matching object.
Take a direct example:
Perform a search that will return a matching object
The import re#search () function returns a Match object: txt = "hello world" x = re.search ("wo", txt) print (x)
Run:
The Match object has properties and methods for retrieving information about searches and results:
Span () returns a tuple containing the matching start and end positions. String returns the string passed to the function group () returns the matching part of the span function in the string
For example: print the location of the first match (start and end positions). The regular expression finds any word that begins with an uppercase "S":
Import re# searches for the uppercase "S" character at the beginning of the word and prints its position txt = "The rain in Spain" x = re.search (r "\ bS\ w +", txt) print (x.span ())
Run:
String function
For example: print the string passed to the function
Import re# returns the string txt = "The rain in Spain" x = re.search (r "\ bS\ w +", txt) print (x.string) group function
For example: print the matching part of a string. The regular expression looks for any word that starts with an uppercase "S"
Import re# searches for the uppercase "w" character at the beginning of the word and prints the word: txt = "hello world" x = re.search (r "\ bw\ w +", txt) print (x.group ())
Run:
Note: if there is no match, None will return the value instead of the matching object.
The above is all the content of the article "what is the knowledge of 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.