<>re Common methods of modules

*
regular expression , Also known as regular expression .( English :Regular
Expression, In code, it is often abbreviated as regex,regexp or RE), A concept of Computer Science . Regular expressions are usually used to retrieve , Replace those that match a pattern ( rule ) Text for .

*
Given a regular expression and another string , We can achieve the following goals :
Whether the given string conforms to the filtering logic of the regular expression ( call “ matching ”);
You can use regular expressions , Get the specific part we want from the string .

*
Regular expressions are characterized by :
flexibility , Very logical and functional ;
The complex control of strings can be achieved quickly in a very simple way ;
For people who are new to contact , It's rather obscure .
re Module operation
stay Python Through the re Module to complete the regular expression operation

match(string[, pos[, endpos]])
string Is the string to be matched pos and endpos Optional parameters , Specifies the start and end positions of the string , The default values are 0 and len( String length ).
# match method : Find from start , One time match re.match(pattern, string, flags=0) result =
re.match("hello", "hellolzt world") print(result, result.group(), type(result))
Match at the beginning of a string pattern, If the match is successful ( Can be an empty string ) Returns the corresponding match object , Otherwise return None.

<>search method

* Find anywhere in the string , Match only once , Return as soon as a match is found
search(string[, pos[, endpos]]) ,string Is the string to be matched pos and endpos
Optional parameters , Specifies the start and end positions of the string . When the match is successful , Return one Match object , If there is no match , Return to None. Scan entire string string, Find and regular expression
pattern First match of ( Can be an empty string ), And return a corresponding match object . If there is no match, return None. re.search(pattern, string,
flags=0) result = re.search("hello", "2018hellolzt world") print(result.group())
<>fullmatch method

* fullmatch(pattern, string, flags=0), yes match Exact matching of functions ( From the beginning to the end of a string )
re.fullmatch(pattern, string, flags=0) result = re.fullmatch("hello", "hello1")
print(result)
string Is the whole and pattern matching , If it is, return the corresponding match object , Otherwise return None.

<>findall method

* Returns all matching substrings in the form of a list , If there is no match , An empty list is returned . findall(string[, pos[, endpos]]),string
String to match pos and endpos Optional parameters , Specifies the start and end positions of the string . findall(pattern, string, flags=0)
result = re.findall("hello", "lzt hello china hello world") print(result,
type(result)) # Back to list
<>split method

* According to the matching substring, the string is split and returned to the list split(string[, maxsplit]),maxsplit Used to specify the maximum number of splits , Do not specify to split all .
re.split(pattern, string, maxsplit=0, flags=0) result = re.split("hello",
"hello china hello world", 2) print(result, type(result)) # Return to split list
<>sub method

* For replacement ,sub(repl, string[, count]),epl It can be a string or a function :
(1) If repl Is a string , Will be used repl To replace each matching substring of a string
(2) If repl It's a function , Method accepts only one parameter (Match object ), And returns a string for replacement .
(3) count Used to specify the maximum number of substitutions , Replace all if not specified . sub(pattern, repl, string, count=0, flags=0)
result = re.sub("hello", "hi", "hello china hello world", 2) print(result,
type(result))
use repl replace pattern Matched content , Maximum match count second

<>iterator method
finditer(pattern, string, flags=0) result = re.finditer("hello", "hello world
hello china") print(result, type(result)) # Return iterator
<>compile method

* compile Function is used to compile regular expressions , Generate a Pattern object compile(pattern, flags=0) pat =
re.compile("hello") print(pat, type(pat)) result = pat.search("helloworld")
print(result, type(result)) # The matching model was compiled
<>flags

* re In some functions of the module flags As an optional parameter , Here are some common ones flag, They actually correspond to binary numbers , You can use them in bits or in combination .flags
May change the behavior of regular expression :
re.I re.IGNORECASE: Case insensitive in matching
re.M re.MULTILINE: “^“ Match string start and ”\n" after ;”$“ matching ”\n" Before and at the end of the string . It is often referred to as multiline mode
re.S re.DOTALL: "." Match any character , Include line breaks . It is often referred to as one-way mode
If you want to use both single line mode and multi line mode , Only the optional parameters of the function need to be changed flags Set to re.I| re.S that will do . result = re.match("hello",
"HeLlo", flags=re.I) print(result) result =
re.findall("^abc","abcde\nabcd",re.M) print(result) result =
re.findall("e$","abcde\nabcd",re.M) print(result) result = re.findall(".",
"hello \n china", flags=re.S) # "." Can match line breaks print(result) result =
re.findall(".", "hello \n china", flags=re.M) # "." Cannot match line breaks print(result)

Technology
©2020 ioDraw All rights reserved
Data storage in memory software engineering —— Development model machine learning —k-means,k-means++ as well as k-means|| algorithm analysis utilize pytorch Training network --- refuse classification ,(resnet18)win10 Blue screen caused by installing shadow system , Termination code :BAD_SYSTEM_CONFIGPython Basic steps of crawling web data Key and difficult points of playing with pointer (1) Gude Haowen serial - You deserve to be an engineer ( time & Event management tools ) Gude Haowen serial - You deserve to be an engineer ( Preface )C language - Catering management system