Python path-common modules (re modules)

Source: Internet
Author: User

a . Regular Expressions

definition: A method that has special meaning symbols grouped together to describe a character or string. Embedded in Python, by

The RE module is implemented, compiled into a series of bytecode, executed by the matching engine written by C.

1. Common matching mode

2. Common re-module functions

(1). Re.match ()

Determines whether the re matches the position of the string at the beginning. Note: This method is not an exact match. When the pattern ends, if the string is left

Remaining characters are still considered successful. If you want an exact match, you can add the boundary match at the end of the expression ' $

Format:

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

Print (Re.match (' com ', ' Comwww.runcomoob '). Group ())

Print (Re.match (' com ', ' Comwww.runcomoob ', re. I). Group ())

   The execution results are as follows:    com    com
(2). Re.search ()

Format:

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

The Re.search function looks for a pattern match within the string, as long as the first match is found and then returns none if the string does not match.

Example:

Print (Re.search (' \dcom ', ' www.4comrunoob.5com '). Group ())

   The execution results are as follows:   4com

(3). Re. FindAll ()

Re.findall traversal matches, you can get all the matching strings in the string and return a list.

Format:

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

p = re.compile (R ' \d+ ')

Print (P.findall (' O1n2m3k4 '))

 The execution results are as follows: [' 1 ', ' 2 ', ' 3 ', ' 4 ']
(4). Some points of attention:

(1) The difference between Re.match and Re.search and Re.findall:

Re.match matches only the beginning of the string, if the string does not begin to conform to the regular expression, the match fails and the function returns none;

and Re.search matches the entire string until a match is found.

(2) greedy match and non-greedy match

*?,+?,??, {m,n}? In front of the *,+, and so on are greedy matches, that is, match as much as possible, after adding the number to make it an inert match


Python path-common modules (re modules)

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.