Python re module and Pythonre Module

Source: Internet
Author: User

Python re module and Pythonre Module
Python re Module 1, compile (pattern): Create mode object

ImportRe

Pat = re. compile ('A')

M = pat. search ('Ba')

# Equivalent to re. search ('A', 'ba ')

Print (m)

# <_ Sre. SRE_Match object; span = (2, 3), match = 'A'>

 

import re
pat = re.compile('a')
m = pat.search('CBA')
Print (m) # None
2. search (pattern, string): searches for the pattern in the string.
import re    
m = re.search('asd','ASDasd')      
print(m)     
#<_sre.SRE_Match object; span=(3, 6), match='asd'>       

The above function returns can be judged in the if Condition Statement:

If pat. search ('asd ', 'asdasd '):

Print ('OK ')

The output is 'OK '.

3. split (pattern, string): splits the string according to the mode, and returns the List,
import re
M = re. split (',','A, s, d, asd') # Separated by commas
print(m)      #['a', 's', 'd', 'asd']
B,
import re
pat = re.compile(',')
m=pat.split('a,s,d,asd')
print(m)      #['a', 's', 'd', 'asd']
C,
import re
m = re.split('[,]+','a,s,d ,,,,,asd')
# Regular Expression matching: [,] +, which is described later
print(m)     #['a', 's', 'd ', 'asd']
D,
import re
m= re.split('[,]+','a,s,d,e, ,,,,,asd',maxsplit=2)
# Maximum number of maxsplit splits
print(m)   
#['a', 's', 'd,e, ,,,,,asd']
E,

ImportRe

Pat = re. compile ('[,] +')

M = pat. split ('A, s, d, e, asd', Maxsplit = 2)

Print (m) # ['A', 's', 'd, e, asd ']

4. findall (pattern, string): returns a match in the form of a list.

ImportRe

C = re. findall ('A','Aasdadsda')

Print (c) # ['A', 'A', 'a']

 

ImportRe

Pat = re. compile ('A')

C = pat. findall ('Aasdadsda')

Print (c) # ['A', 'A', 'a']

 

ImportRe

Pat = re. compile ('[A-Z] +')

C = pat. findall ('Aasdadsda')

Print (c) # ['asd ', 'dsd']

 

ImportRe

Pat = re. compile ('[A-Z]')

C = pat. findall ('Aasdadsda')

Print (c) # ['A', 's', 'D', 'D', 's', 'D']

 

ImportRe

Pat = re. compile ('[A-Za-z] +')

# Regular Expression matching: '[A-Za-z] +' matches all words

C = pat. findall ('Asd adsda')

Print (c) # ['aasd ', 'adsda']

 

ImportRe

Pat = re. compile ('[A-Za-z]')

C = pat. findall ('Asd adsda')

Print (c)

# ['A', 'A', 's', 'D', 'A', 'A', 'D', 's', 'D', 'a']

5. sub (pat, repl, string): replace pat matching items with repl.

ImportRe

C = re. sub ('A','A','Asd adsda')

Print (c) # AASD ADSDA

 

ImportRe

Pat = re. compile ('A')

C = pat. sub ('A','Asd adsda')

Print (c) # AASD ADSDA

 

ImportRe

Pat = re. compile (R 'www \. (. *) \ .. {3 }')

'''

InPythonOfStringAdd'R'To tell the editor thatStringYesRaw string, Do not translate the backslash'\'.

For example,\ NInRaw stringIs two characters,\AndNInstead of line breaks.

Because regular expressions and\There will be conflicts. Therefore, when a string uses a regular expression, it is best to add'R'.

In most programming languages, the regular expression uses\"As an escape character, which may cause backlash troubles.

If you need to match the character"\", The regular expression in the programming language will require4Backslash"\\\\":

The first two and the last two are used to convert them into backslashes in the programming language, convert them into two backslashes, and then escape them into a backslash in the regular expression.

PythonThe native string in this example can solve this problem well.R"\\.

Similarly, matching a number\ DCan be writtenR"\ D".

With the native string, you no longer have to worry about missing the backslash, and the written expression is more intuitive.

Remember this sentence:

When a string uses a regular expression, it is best to add'R', So you no longer have to worry about missing the backslash, and the written expression is more intuitive.

'''

Ret = pat. match ('Www .dxy.com'). Group (1)

Print (ret) # dxy

A = re. sub (R 'www \. (. *) \ .. {3 }',R' \ 1','Hello, www.dxy.com')

Print (a) # hello, dxy

B = pat. sub (R' \ 1','Hello, www.dxy.com')

Print (B) # hello, dxy

'''

R'\ 1'Is the meaning of the first group

Find the compliant"Www.dxy.com", Get Group1Replace the entire match with the string

'''

 

 

Pat = re. compile (R' (\ w + )')

S ='Hello world! Hello hz! '

D = pat. findall ('Hello world! Hello hz! ')

Print (d)

# [('Hell ', 'O'), ('worl', 'D'), ('hell', 'O'), ('h ', 'Z')]

C = pat. sub (R' \ 2 \ 1', S)

Print (c) # ohell dworl! Ohell zh!

'''

Obtain a group through regular expression1(Hell), Group2(O), And then passSubReplace. Group1Replacement Group2, Group2Replacement Group1, Change location

'''

6. escape (string): escape special strings in the string

ImportRe

A = re. escape ('Www .dxy.com')

Print (a) # www \. dxy \. com

7. group: Obtain the matching items of the sub-mode (group ).

In the above functions, only match and search have the group method, and other functions do not.

ImportRe

Pat = re. compile (R 'www \.(.*)\.(.*)')

# Use () to represent one group and two groups

M = pat. match ('Www .dxy.com')

Print (m. group ())

# The default value is 0, indicating that the entire string is matched. Result: www.dxy.com

Print (m. group (1 ))

# Returns the string that matches Group 1. Result: dxy

Print (m. group (2 ))

# Returns the string matching the given group 2. Result: com

8. start: Specifies the start position of a group matching item.

ImportRe

Pat = re. compile (R 'www \.(.*)\.(.*)')

M = pat. match ('Www .dxy.com')

Print (m. start (2) # index starting from group 2. Result: 8

9. end: end position of the given group match

ImportRe

Pat = re. compile (R 'www \.(.*)\.(.*)')

M = pat. match ('Www .dxy.com')

Print (m. end (2) # end index of Group 2. Result: 11

10. span: Specifies the start and end positions of group matching items.

ImportRe

Pat = re. compile (R 'www \.(.*)\.(.*)')

M = pat. match ('Www .dxy.com')

Print (m. span (2 ))

# Start and end indexes of Group 2. Result: (8, 11)

 

 

 

 

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.