Python beginners (5) and python beginners
14. Regular Expressions
Regular Expressions are powerful tools used to process strings, but they are not unique to Python. Many programming languages support regular expressions, which are rarely used;
The Regular Expression in Python is in the re module;
The quantifiers in Python are greedy by default, and always try to match as many characters as possible. If they are not greedy, they always try to match as few characters as possible (for example: regular Expression "AB *" if used to find "abbbc", "abbb" is found, and "a" is found if "AB *" is used as a non-Greedy quantizer ".);
Like most programming languages, regular expressions use "\" as escape characters, which may cause backlash troubles. If you need to match the character "\" in the text, four Backslash "\" will be required in the regular expression expressed in programming language "\\\\": the first two and the last two are used to convert them into backslashes in the programming language, convert them into two backslashes, and then escape them into a backslash in the regular expression. The native string in Python solves this problem well. The regular expression in this example can be represented by r.
(Most of the regular expressions in this article are used for reference at http://www.cnblogs.com/huxi/archive/2010/07/04/1774253.html. If there is any error, please confirm it)
The following describes several common methods in the re module:
>>> Import re >>> pattern = re. compile ('python') # compile compiles strings as regular expressions >>> result = pattern. search ('Hello python! ') >>> Result <_ sre. SRE_Match object; span = (6, 12), match = 'python' >>> result. group () 'python' >>>>> result = re. match ('A', 'abc') # match starts from the beginning of the string and matches >>> result <_ sre. SRE_Match object; span = (0, 1), match = 'A' >>> result. group () # does not directly return matched strings. You must use the group () method 'A' >>> result = re. match ('A', 'abc') >>> result. group () # Traceback (most recent call last): File "<pyshell #6>", line 1, in <module> result. group () AttributeError: 'nonetype 'object has no attribute 'group' >>>>> result = re. search ('python', 'abcpythondef ') # search for a match in the full text of a string, or directly return a matched string >>> result <_ sre. SRE_Match object; span = (3, 9), match = 'python' >>> result. group () 'python' >>>>> result = re. findall ('python', 'abc python def python ghi') >>> result ['python', 'python'] >>>>> result = re. sub ('C', 'z', 'click', 1) # Replace the matched string with the specified string. The parameters are regular expressions in sequence, the string to be replaced after the match is successful. The original string is replaced. >>> result # returns the replaced string 'zlick' >>>>> result = re. split ('A', '1a2a3a4guyuyun ') # Use the matched string as the string separator and return the list of strings separated >>> result ['1', '2 ', '3', '4guyuyun '] >>>