Python:re Module

Source: Internet
Author: User

The main is to learn the use of Re module, the regular expression of learning ... I have seen, is the use of less, so do not remember what, when used, but also through my hand-recorded regular notes ..... If you are crawling pages, you can use the BS4 module, this is more convenient, but the regular is very powerful (~ ̄▽ ̄) ~

Look at the following article to make notes

https://mp.weixin.qq.com/s?__biz=MzI5MDQ2NjExOQ==&mid=2247483970&idx=1&sn= ee3c82889fcec5779a0f6441f0a2f70a&chksm= ec1e326adb69bb7ccd9a972f50456bd7bc79f68123c165f1c9e7c84a0a425cfdbf5d021d28d2&mpshare=1&scene=23& Srcid=1223rwdt94lsyy9f2y7apob7#rd

Re.compile (pattern[, flag])

Function: To generate a regular expression object

pattern is the regular expression to compile, and flag is the flag bit in the image below .

Match (string[, pos[, Endpos])

parameter interpretation:string is the original string used for matching, POS is the index of the text expression start search, endpos text expression End Search index

Successful use of the MATCH function returns an object that contains functionality:

The group ([Group1, ...]) method is used to obtain one or more grouping matching strings, which can be used directly when the entire matched substring is to be obtained, either group () or group (0);

The start ([group]) method is used to get the starting position of the substring in the entire string (the index of the first character of the substring), the default value of the parameter is 0;

The end ([group]) method is used to get the end position of the grouped matched substring in the entire string (the index of the last character of the substring + 1), the default value of the parameter is 0;

The span ([group]) method returns (Start (group), End (group)).

" QWE123ASD " Import re>>> pattern = re.compile ('[a-z]+')>>> obj = Pattern.match (str)

Search (string[, pos[, Endpos])

for a match, If the match returns a match object successfully, none is returned if unsuccessful

FindAll (string[, pos[, Endpos])

parameter interpretation:string is the original string used for matching, POS is the index of the text expression start search, endpos text expression End Search index

FindAll returns all matched substrings in a list, and returns an empty list if there is no match.

Finditer (string[, pos[, Endpos])

The behavior of the Finditer method is similar to the behavior of FindAll, and it also searches the entire string for all matching results. But it returns a match object.

Split (string[, Maxsplit])

The maxsplit is used to specify the maximum number of splits and does not specify that all will be split. (Python has a built-in method that also split)

Sub (repl, string[, Count])

REPL can be either a string or a function, string is the original string used for matching, and count is used to specify the maximum number of replacements, not all when specified.

if repl is a string, it uses REPL to replace each matched substring of the string and returns the substituted string, and Repl can also refer to the grouping using the ID, but not the number 0;

if repl is a function, this method should only accept one argument (the Match object) and return a string for substitution (the returned string cannot be referenced in a group).

The Sub function returns the replaced string.

The main repl is the function needs attention

Subn (REPL, string[, Count])

parameters are consistent with sub parameters

Subn returns a tuple , the first element is the result of using the Sub method, and the other is the number of substitutions

when you are not using compile, just precede the function with re. and the first argument is a regular expression, for example: Re.search ("\d", "myh0st")

Python:re Module

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.