7.python Regular Expression re module

Source: Internet
Author: User

The common methods of the RE module are:

Re.findall () Returns a list of all strings that can be matched by a regular expression in a string.

Usage Re.findall (regular expression, string).

S1 = "Sadjhjafdsajkhjsdaysadsadduayu"

For example, if you want to match a string from the long string above, the one that has a character followed by a letter U.

Print Re.findall (' a.u ', S1)

>>>[' Ayu ']


Re.finditer () acts like FindAll, but Finditer returns not a list but an iterator object, and the Finditer method is recommended if iterative operations are required.


Re.search () follows the regular expression in the string to find the string to match, finds only the first match, and returns a match object containing information that matches the regular expression. We can do this by executing the object. The group method goes to get the string that matches, and returns a none if it finds a string to match.

Usage: Re.search (regular expression, string, flag)

S1 = "Hamasaki Ayumi"

Print Re.search (' a.u ', S1). Group ()

>>>ayu

The Re.match () string is followed by regular expressions to find the strings needed for the match, and unlike search, match only matches from the beginning of the string! If the string starts not conforming to the regular expression, the match fails!

Usage is the same as search.


Re.split () splits the string by the specified regular expression.

Cases:

Use all strings as delimiters.

s = "A1b2c3d4e5"

Print Re.split (R ' \d ', s)

>>>[' A ', ' B ', ' C ', ' d ', ' e ', ']


Re.sub () Replace, use a regular expression to match the specified content in the string, and then replace it with the specified string.

s = "A1b2c3d4e5"

Print re.sub (R ' [A-z] ', ' Ayu ', s)

>>>ayu1ayu2ayu3ayu4ayu5

The Re.sub () function also has a parameter that you can set how many times to replace.

For example, just want to replace the first three matches.

s = "A1b2c3d4e5"

Print re.sub (R ' [A-z] ', ' Ayu ', s,3)

>>>ayu1ayu2ayu3d4e5


Re.subn (), like a sub, is the ability to replace string content with a regular, but SUBN returns a tuple that is not just a replacement string, but also contains the contents of the string that have been replaced several times.

For example:

s = "A1b2c3d4e5"

Print re.subn (R ' [A-z] ', ' Ayu ', s)

>>> (' Ayu1ayu2ayu3ayu4ayu5 ', 5)


Re.compile () compilation, you can compile a regular expression into a regular expression object. It is possible to compile regular expressions that are often used as regular expression objects, which can improve some efficiency.



Two. The grouping of Python regular expressions.

Python is commonly used in the grouping method is probably divided into two kinds.

Method 1:

Print Re.search (R ' (^\d{3,4})-(\d{3,8}) ', ' 010-123456 '). Group (0)

>>>010-123456

Print Re.search (R ' (^\d{3,4})-(\d{3,8}) ', ' 010-123456 '). Group (1)

>>>010

Print Re.search (R ' (^\d{3,4})-(\d{3,8}) ', ' 010-123456 '). Group (2)

>>>123456

If a group is defined in a regular expression, a substring can be extracted with the group () method on the match object.

Notice that group (0) is always the original string, group (1), Group (2) ... Represents the 1th, 2 、...... Substring.


Method 2:

Named groupings

Print Re.search (? p<zone_num>^\d{3,4})-(? p<phone_num>\d{3,8}) ', ' 010-123456 '). Group ("Zone_num")

>>>010

Print Re.search (? p<zone_num>^\d{3,4})-(? p<phone_num>\d{3,8}) ', ' 010-123456 '). Group ("Phone_num")

>>>123456

This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1910537

7.python Regular Expression 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.