Using regular expressions in Python

Source: Internet
Author: User

One. Escape character

1. Escape characters in Python

(1) The difference between \\n and \ n

(2) R "\next" is escaped by R

(3) Combination of "\\\\d" and "\\d"

2. Escape characters in regular expressions

(1) \\n means match \ n this character

(2) [(), +, *,/,.,?, $] These symbols will be present in []

(3) "\ (" indicates a match (this symbol

Dual RE Module

1. String Matching

(1) Re.findall ("Regular expression", "string") returns a list of all contents that match to, if there is no match to the content returned [].

(2) Re.search ("Regular expression", "string") returns the match to the result of the object, if there is no match to the content returned to none

(3) Re.match ("Regular expression", "string") returns the contents of the same as search, but at the beginning of the string must match to the content, if the beginning does not match to return none

2. Replacement and cutting

(1) re.sub ("Regular expression", "replaced content", "string", number (default all replaced)) returns a replaced string

(2) re.subn ("Regular expression", "replaced content", "string", number (default replace All)) returns a tuple that returns the number of strings and replacements that have been replaced

3. Advanced method

(1) re.compile ("Regular expression") (Time efficiency) saves time: used only if one of the same regular expressions is used more than once

(2) Re.findter ("Regular expression", "string") returns an iterator that, through a for loop, gets the object that matches all of the results. Then use Group () to print with each result

Three. Some special usages of regular expressions used in Python

1.findall+ Group priority to display the contents of the group, in order to remove the group priority, in the group to add (?:)

2.search+ Group (n), you can get the content that group () matches to

3.split+ grouping preserves the split content within a group

4. Group naming

(1).

ret = Re.search ("< (? p<name>\w+) >\w+</(? P=name) >","<a>hahaha</a>")Print(Ret.group ())Print(Ret.group ("name"))Print(Ret.group (1))#Print (Ret.group (2))ret = Re.search (r"< (\w+) >\w+</\1>","<a>hahaha</a>")Print(Ret.group ())Print(Ret.group (1))
View Code

Four. Random module

1. Random Decimals

(1). Random decimals in the range of Random.random () 0-1

(2) Random decimals in any range of random.uniform (1,5)

2. Random integers

(1) random.randint [+] random integers in the range including 2

(2) Random.randrange [+] random integers in the range not including 2

(3) Random.randrange (1,10,2) [1,10) does not contain 10 random odd numbers within the range

3. Random Extraction

(1) Random.choice (list) randomly extracts a value from the list

(2) random.sample (lst,n) randomly extracts n values

(3) Random.stuffle (list) on the basis of the original list of disorderly order

Random extraction of a value lst randomly= [A.'AAA',('Wahaha','qqxing')]ret=Random.choice (L)Print(ret) randomly extracting multiple values ret= Random.sample (lst,2)Print(ret) disorderly sequence on the basis of the original list of LST= [A.'AAA',('Wahaha','qqxing')]random.shuffle (LST)Print(LST)

Using regular expressions in Python

Related Article

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.