Article Directory
Preface
Python provides the re module for regular expression operations. In the implementation, you can use a variety of methods provided by the re module to process strings. When the re module is used, you need to apply the import statement to import it, that is
import re
I don't know how other people feel about starting to learn regular expressions, anyway, I have been confused for a long time. Let's talk about the methods provided by the re module in detail below.
1. Methods of matching strings
The matching string can use the match(), search() and findall() methods provided by the re module
1. Use the match() method to match
The match() method is used to match from the beginning of the string. If the match is successful at the starting position, it returns the Match object, otherwise it returns None. The syntax format is as follows:
re.match(pattern,string,[flags])
pattern: represents the pattern string, converted from the regular expression to
be matched string: represents the string to be matched
flags: optional parameter, represents the flag bit, used to control the matching method
For example, whether the matching string starts with "hg", case-insensitive:
Implementation code:
Run result:
Explain the results of the operation. From the above results, the string string starts with "hg_" (not case sensitive), so a Match object is returned, and the string str does not start with "hg_". When using match( When the) method starts to match from the beginning of the string, the letters that do not meet the conditions will not be matched again, so it returns "None" where the
Match object contains the matching value (obtained by the start() method) and matching data (Use the end() method to obtain), the span() method can return the tuple of the matching position, and the string to be matched can be obtained through the string attribute.
Example:
running result:
2. Use the search() method to match
The search() method is used to search for the first matching value in the entire string. If the match is successful, it returns the Match object, otherwise it returns None. The
syntax format is as follows:
re.search(pattern,string,[flags])
For example, search for a string starting with "hg", regardless of case.
Implementation code:
Run result:
It can be seen from the run result that the search() method does not necessarily only search for the starting position of the string when searching for a string , Other locations can also be searched
3. Use the findall method to match
The findall() method is used to search for all strings that meet the regular expression in the entire string and return it in the form of a list. If the match is successful, it returns a list containing the matching structure, otherwise it returns an empty list. The
syntax format is as follows:
re.findall(pattern,string,[flags])
For example, search for a string beginning with "hg", regardless of case.
Implementation code:
Run result:
2. Replace the string
The sub() method can be used to replace strings. The syntax format is as follows:
re.sub(pattern,repl,string,count,flags)
pattern: represents the pattern string, converted from the regular expression to be matched
repl: represents the string
to be replaced string: represents the string to be matched
count: optional parameter, represents the maximum number of replacements after pattern matching, the default value is 0, means to replace all matching
flags: optional parameter, means flag bit, used to control the matching method
For example, hide personal information:
implementation code:
run results:
Three. Use regular expressions to split strings
The split() method is used to split a string according to a regular expression and return it in the form of a list. The syntax format is as follows:
re.split(pattern,string,[maxsplit],[flags])
maxsplit: optional parameter, indicating the maximum number of splits
For example, extract the request address and various parameters from a given URL address.
Implementation code:
Run result:
I hope this is helpful to everyone. I feel that regular expressions for strings are still very difficult for beginners, but come on, the more difficult the more forwardつ﹏⊂
come on! Come on |