Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.
This paper has participated inProject DigginTo win the creative gift package and challenge the creative incentive money.
Review and
Regular Expression is used to describe a Pattern of string matching. Python, C#, PHP, and Javascript all support Regular Expression.
We all know that regular expressions can be used to check if a string contains a substring, to extract a match from a string, or to perform operations on matched substrings in a string.
We have studied two episodes in the past. The details are as follows
-
Regular expression grammar, learning the regular representation of the common language method and the principle of execution engine learning
-
Complie compiles regular objects, understanding the matching pattern constants supported by Python re module, re.compile() method of regular object compilation
The re module, Python’s built-in library, contains nine constants and 12 methods
We’ve already learned how to compile regex, and in this installment we’ll continue to learn how to use the common methods of match lookup, replace, and split in the RE module
1. The search returns a match
methods | role |
---|---|
res.search(pattern,string,flags) | Scans the entire string for a regular expression match |
res.match(pattern,string,flags) | Matches the regular expression from the beginning of the detected string. |
res.fullmatch(pattern,string,flags) | The entire detected string matches the regular expression |
1.1 re. The match ()
res.match(pattern,string,flags=0)
Parameters:
- Pattern: indicates a regular expression
- String: indicates the matched string
- Flags: represents the matching pattern specified by the compiled regular expression (9 constants)
Function:
- Matches the regular expression from the beginning of the matched string
- The match function returns None if the match from the starting position is unsuccessful
- If a match can be made from the starting position, a _sre.sre_match object is returned
- The _sre.sre_match object contains substrings matching spa(n) for the start and end positions of the n+1 group and group(n) for the n+1 group
For example 🌰 :
Match1 = re.match(pattern,"Juejin") Match2 = re.match(pattern,"hello Juejin") print(" hello Juejin") print(" hello Juejin","hello Juejin") print(" Hello Juejin","hello Juejin") print(" Hello Juejin","hello Juejin") print(" Hello Juejin","hello Juejin")Copy the code
Running results:
1.2 re. The search ()
res.search(pattern,string,flags=0)
Parameters:
- Pattern: indicates a regular expression
- String: indicates the matched string
- Flags: represents the matching pattern specified by the compiled regular expression (9 constants)
Function:
- Scans the entire string to match the regular expression
- If there is no match, the search function returns None
- If a matching string is found, the search function returns the first matching object, _sre.sre_match
- The _sre.sre_match object contains substrings matching spa(n) for the start and end positions of the n+1 group and group(n) for the n+1 group
For example 🌰 :
Match1 = re.search(pattern,"Juejin") print(" match1 :",match1) print(" match1 :",match1) print(" match1 :",match1 = re.search(pattern,"hello Juejin") print(" match2: ",match2)Copy the code
Running results:
1.3 re. Fullmatch (pattern, string, flags)
res.fullmatch(pattern,string,flags=0)
Parameters:
- Pattern: indicates a regular expression
- String: indicates the matched string
- Flags: represents the matching pattern specified by the compiled regular expression (9 constants)
Function:
- The entire string matches the regular expression exactly
- If the string does not exactly match the regular expression, None is returned
- If a match is found, the _sre.sre_match object containing the match information is returned
- The _sre.sre_match object contains substrings matching spa(n) for the start and end positions of the n+1 group and group(n) for the n+1 group
For example 🌰 :
Match1 = re.fullmatch(pattern,"Jue") Match2 = re.fullmatch(pattern,"Juejin") print(" match ",match2)Copy the code
Running results:
2. Multiple matches are returned
methods | role |
---|---|
re.findall(pattern,string,flags) | Returns, as a list, all substrings in the search string that match the regular expression |
re.finditer(pattern,string,flags) | Returns all substrings in the search string that match the regular expression as iterators |
2.1 re. The.findall ()
re.findall(pattern,string,flags)
Parameters:
- Pattern: indicates a regular expression
- String: indicates the matched string
- Flags: represents the matching pattern specified by the compiled regular expression (9 constants)
Function:
- Scans the entire string for a regular expression match
- Find all substrings that satisfy the regular expression
- The results are returned as a list
For example 🌰 :
Import re # findAll scans the entire string to match the regular expression, Match1 = re.findall(pattern," Hello Juejing,love Juejin") print(" string matching multiple results :",match1)Copy the code
Running results:
2.2 re. Finditer ()
re.finditer(pattern,string,flags=0)
Parameters:
- Pattern: indicates a regular expression
- String: indicates the matched string
- Flags: represents the matching pattern specified by the compiled regular expression (9 constants)
Function:
- Scans the entire string for a regular expression match
- Find all substrings that satisfy the regular expression
- The result is returned as an iterator
For example 🌰 :
Import re # finditer scans the entire string to match the regular expression, Match1 = re.finditer(pattern," Hello Juejing,love Juejin") Print (" string matches multiple results :",list(match1))Copy the code
Running results:
3. Segmentation method
re.split(pattern,string,maxsplit=0,flags=0)
Parameters:
- Pattern: indicates a regular expression
- String: indicates the matched string
- Maxsplit: indicates the maximum number of splits
- Flags: represents the matching pattern specified by the compiled regular expression (9 constants)
Function:
- Regular expressions split strings
- The segmented substrings are combined into a list and returned
For example 🌰 :
Import re # split regular expression for matching string split, return a list pattern = "," Match1 = re.split(pattern,"Juejin1,Juejin2,Juejin3") print(" string split :",list(match1))Copy the code
Running results:
4. Replacement method
methods | role |
---|---|
re.sub() | Replace the count substring in the string that matches the regular expression and return the replaced string |
re.subn() | The string in the replacement string that matches the regular expression, returns the replacement string and the number of substitutions |
4.1 re. Sub ()
re.sub(pattern,repl,string,count=0,flags=0)
Parameters:
- Pattern: indicates a regular expression
- Repl: Represents the string to be replaced. It can also be a function
- String: indicates the matched string
- Count: indicates the maximum number of times to control the replacement. If count is 0, it indicates all replacement
- Flags: represents the matching pattern specified by the compiled regular expression (9 constants)
🌟 : When repl is a function, there can only be one entry: Match Match object
Function:
- The substring of the regular expression that matches the string
- Replace the matched substring with repL and return the replaced string
- When count is not specified, all is replaced by default
For example 🌰 :
Import re # sub regular expression for matching string substitution, return the replaced string pattern = "," Match1 = re.sub(pattern,repl,"Juejin1,Juejin2,Juejin3") print(" match1 ",match1 Re.sub (pattern,repl,"Juejin1,Juejin2,Juejin3",1) print(" match2 ")Copy the code
Running results:
4.2 re. Subn ()
re.subn(pattern,repl,string,count=0,flags=0)
Parameters:
- Pattern: indicates a regular expression
- Repl: Represents the string to be replaced. It can also be a function
- String: indicates the matched string
- Count: indicates the maximum number of times to control the replacement. If count is 0, it indicates all replacement
- Flags: represents the matching pattern specified by the compiled regular expression (9 constants)
Function:
- The substring of the regular expression that matches the string
- Replaces matched substrings with repl and returns a tuple (string, number of substitutions)
For example 🌰 :
Import re # subn regular expression for matching string substitution, return the replaced string and times pattern = "," Match1 = re.subn(pattern,repl,"Juejin1,Juejin2,Juejin3") print(" match1 ", "match1" Re.subn (pattern,repl,"Juejin1,Juejin2,Juejin3",1) print(" match2 ")Copy the code
Running results:
5. Other methods
methods | role |
---|---|
re.escape() | Escape special characters in regular expressions |
re.purge() | Clear the regular expression cache |
5.1 re. The escape ()
re.escape(pattern)
Parameters:
- Pattern: indicates a regular expression
Function:
- Escape strings with special meanings in regular expressions
- Escape includes characters other than ASCII characters, numeric values, and underlined (_)
🌟 suggestion: When using the re.escape() method, it is very easy to convert errors. It is recommended to use this method as little as possible and replace it with r””
For example 🌰 :
Print (" After escaping :",re.escape(pattern)) # search for a match Match1 = re.search(pattern,"Juejin1, Juejin1 *,Juejin3") print(" match1 ")Copy the code
Running results:
5.2 re. Purge ()
Function:
- Clear the regular expression cache
Internal code implementation
Convert the regex compiled by re.compile() into a ‘NoneType’ object
conclusion
In this issue, we have studied in detail the common methods of RE module, such as single matching, multiple matching, replacement, segmentation and so on
This makes it easy to efficiently use regular expressions in Python code on a daily basis
That’s the content of this episode. Please give us your thumbs up and comments. See you next time