Python Beginner (Fri)

Source: Internet
Author: User

14. Regular Expressions

A regular expression is a powerful tool for working with strings, but it is not unique to Python, and many programming languages support regular expressions, and there is little difference in usage;

The regular expression in Python is in the re-module;

The number of words in Python is greedy by default, always trying to match as many characters as possible, and not greedy, instead, always trying to match as few characters as possible (for example: regular expression "ab*" if used to find "ABBBC", will find "abbb", and if use non-greedy quantity word "ab*", The "a" will be found. );

As with most programming languages, "\" is used as an escape character in regular expressions, which can cause a backslash to be plagued. If you need to match the character "\" in the text, then 4 backslashes "\\\\" will be required in the regular expression expressed in the programming language: the first two and the last two are used to escape the backslash in the programming language, converted to two backslashes, and then escaped in the regular expression into a backslash. The native string in Python solves this problem well, and the regular expression in this example can be expressed using R "\ \".

(Bengwenzheng expression Part of the majority of reference to http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html, if wrong, please correct)

Here are a few common methods in the RE module:

>>>ImportRe>>> pattern = Re.compile ('python' # Compile the string as a regular expression to compile >>> result = Pattern.search ('Hello python!')>>>result<_sre. Sre_match object; Span= (6,), match='python'>>>>Result.group ()'python'>>> >>> result = Re.match ('a','ABC' # Match is matched from the beginning of the string >>>result<_sre. Sre_match object; span= (0, 1), match='a'>>>>Result.group () # does not directly return a string that matches successfully, and needs to use the group () method 'a'>>> result = Re.match ('a','DABC')>>>result>>>Result.group () # no match succeeded Traceback (most recent call last): File"<pyshell#6>", Line 1,inch<module>Result.group () Attributeerror:'Nonetype'object has no attribute'Group'>>> >>> result = Re.search ('python','Abcpythondef' # Searches for a match in the full text of the string, and does not directly return a string that matches the success >>>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 string with the specified string with a successful match, followed by the regular expression, the string to be replaced after the successful match, the original string, the number of replacements >>>result # returns the replaced string 'Zlick'>>> >>> result = Re.split ('a','1a2a3a4guyuyun' # matches a successful string as a string delimiter, returning a delimited list of strings >>>result['1','2','3','4guyuyun']>>>

Python Beginner (Fri)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.