Python Re Module summary

Source: Internet
Author: User

Objective:

My environment windows 7 -bit,python2.7

What is re:

Regular expression abbreviation, meaning regular expressions, is One of the many modules of python

Re uses:

Extract the desired text fragment from the selected batch in the text

Re type:

Divided into DFA ( deterministic with poor state automata ) and NFA ( indeterminate with poor state automata )

Re installation:

Open DOS;CD to pip directory; Enter command pip install re

Re common methods:

1.re.compile (pattern, flags=0)

Pattern is of type str, example:pattern = r' ^.*?$ '

2.re.findall (Pattern, string, flags=0)

Return a list of all non-overlapping matches in the string. Returns a list of all non-overlapping strings in a string .

Example 1:print Re.findall (R' (s) (d) ', ' GSD SD FSA GGH SD HF SDGF ')

Results: [(' s ', ' d '), (' s ', ' d '), (' s ', ' d '), (' s ', ' d ')]

Example 2:print Re.findall (R ' (s) d ', ' GSD SD FSA GGH SD HF SDGF ')

Result: [' s ', ' s ', ' s ', ' s ']

Example 3:print re.findall (R ' SD ', ' GSD SD FSA GGH SD HF SDGF ')

Result: [' SD ', ' SD ', ' SD ', ' SD ']

Purpose: Extract links in the source code of the Web page, etc.

3.re.split (Pattern, String, maxsplit=0, flags=0)

Split the source string by the occurrences of the pattern,

Returning a list containing the resulting substrings. Back to List

Example: Print Re.split (R ' s ', ' JSJKJOISHIOSHUISGUUSNJSHBSG ')

Results: [' j ', ' Jkjoi ', ' hio ', ' hui ', ' guu ', ' NJ ', ' HB ', ' G ']

Purpose: Divide large pieces of text into small pieces that are easy to handle

4.re.match (Pattern, string, flags=0)

Try to apply the pattern at the start of the string, returning

A match object, or None if no match was found. Returns an object similar to a pointer

5.re.search (Pattern, string, flags=0)

Scan through string looking for a match to the pattern, returning

A match object, or None if no match was found. Returns an object similar to a pointer

Reference: the IDLE help command;

PostScript: If there are errors and omissions, welcome correction, there is time to update

This article is Bo Master original, reproduced please indicate the source, and @ My heart fly 2015, thank you for your cooperation!

Python Re Module summary

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.