Python Regular Expressions

Source: Internet
Author: User

Regular expressions

Strings are the most data structure involved in programming, and the need to manipulate strings is almost ubiquitous. For example, to determine whether a string is a legitimate email address, although it can be programmed to extract the substring before and after, and then determine whether it is coarse and the domain name, but this is not only cumbersome, and the code is difficult to reuse.

A regular expression is a powerful weapon used to match strings. Its design idea is to use a descriptive language to define a rule for a string, and any string that conforms to the rule, we think it "matches", otherwise the string is illegal.

So the way we judge whether a string is a legitimate email is:

1. Create a regular expression that matches the email;

2, use the regular expression to match the user's input to determine whether it is legal

Because regular expressions are also represented by strings, we first need to know how to use characters to describe a character

Special characters

Variable-length characters

Advanced

Re module

Since the Python string itself is also escaped with \, pay special attention to: s= ' ASDF\\ASDF '

Therefore, we strongly recommend that you use the Python R prefix without considering escaping the problem.

function Re.match(Re.match tries to match a pattern from the starting position of the string, and if the match is not successful, match () returns none)

Re.match (Pattern, string, flags=0)

The function Re.search scans the entire string and returns the first successful match, otherwise none is returned (Re.search (pattern, String, flags=0))

function Re.findall returns all matched strings as a list (Re.findall (pattern, string[, flags]))

extracting substrings In addition to simply determining whether or not to match, regular expressions also have the powerful function of extracting substrings. The grouping to extract is represented by ()

^ (\d{3}-(\d{3,8}$) two groups are defined, and the area code and native number can be extracted directly from the substring .

Greedy match

A regular match is a greedy match by default, which is to match as many characters as possible.

Replace

Python's Re module provides re.sub to replace matches in a string (Re.sub (Pattern, Repl, String, max=0))

Modifiers for regular expressions

Python Regular Expressions

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.