Python's Re module

Source: Internet
Author: User

Python's Re Module 1, compile (pattern): Creating a Schema Object

Import RE

Pat = re.compile (' A ')

m = Pat.search (' CBA ')

#等价于re. Search (' A ', ' CBA ')

Print (m)

#<_sre. Sre_match object; Span= (2, 3), match= ' A ' >

Re
Pat = Re.compile (' a ')
m = Pat.search (' CBA ')
Print (m) #没有匹配到, returns none
2. Search (pattern,string): Finding Patterns in strings
m = Re.search (' ASD ',' ASDASD '

The above function returns can be judged in the IF condition statement:

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

Print (' OK ')

The run output is ' OK '.

3. Split (pattern,string): Splits the string according to the pattern, returns the List A,
Re
M=re.split (', ', 'a,s,d,asd ') #以逗号为分界
Print (m) #[' a ', ' s ', ' d ', ' ASD '
B
Re
Pat = Re.compile (', ')
M=pat.split (' A,S,D,ASD ')
Print (m) #[' a ', ' s ', ' d ', ' ASD '
C
Re
m = re.split (' [,]+ ',' a,s,d,,,,, asd ')
#正则匹配: [,]+, explained later
Print (m) #[' a ', ' s ', ' d ', ' ASD '
D
Re
m= re.split (' [,]+ ',' a,s,d,e,,,,,, asd ', maxsplit=2)
#maxsplit最多分割次数
#[' A ', ' s ', ' d,e,,,,,, ASD ']
E

Import RE

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 list form

Import RE

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

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

Import RE

Pat = Re.compile (' a ')

c = Pat.findall (' AASDADSDA ')

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

Import RE

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

c = Pat.findall (' AASDADSDA ')

Print (c) # [' ASD ', ' DSD ']

Import RE

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

c = Pat.findall (' AASDADSDA ')

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

Import RE

Pat = re.compile (' [a-za-z]+ ')

#正则匹配: ' [a-za-z]+ ' matches all words

c = Pat.findall (' aasd adsda ')

Print (c) # [' Aasd ', ' ADSDA ']

Import RE

Pat = re.compile (' [a-za-z] ')

c = Pat.findall (' aasd adsda ')

Print (c)

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

5. Sub (pat,repl,string): replace Pat match with REPL

Import RE

c = re.sub (' A ',' A ',' aasd adsda ')

Print (c) #AASD ADSDA

Import RE

Pat = Re.compile (' a ')

c = pat.sub (' A ',' aasd adsda ')

Print (c) # AASD ADSDA

Import RE

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

‘‘‘

in the Python of the string Front Plus ' R ' to tell the editor that this string is a Raw String , do not translate the backslash ' \ '.

For example, \ n in the Raw String , which is two characters, \ and the N instead of being translated to a newline character.

because regular expressions and \ There is a conflict, so when a string uses a regular expression, it is best to precede it with ' R '.

In most programming languages, the regular expression uses the " \ "As an escape character, this can cause a backslash to haunt.

If you need to match the characters in the text " \ , then the regular expression in the programming language will require 4 a backslash " \\\\ " :

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

Python is a good solution to this problem, the regular expression in this example can be used R " \\ "said.

Similarly, a \\d that matches a number can be written as R"\d".

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

It is better to remember this sentence:

when a string uses a regular expression, it is best to precede it with ' R ' So you don't have to worry about missing the backslash, the 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 ' It means the first group.

a regular match is found for the rule-compliant "Www.dxy.com" , get the group 1 string to replace the entire match

‘‘‘

Pat = Re.compile (R ' (\w+) (\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!

‘‘‘

get the group by regular 1 ( Hell ), group 2 ( o ), and then through Sub to replace. Group 1 replace Group 2, group 2 Replace group 1, swap position

‘‘‘

6. Escape (String): Escape a special string inside a string

Import RE

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

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

7. Group: Get the matching of sub-pattern (group)

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

Import RE

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

#用 () represents 1 groups, 2 groups

m = Pat.match (' www.dxy.com ')

Print (M.group ())

#默认为0 that represents the entire string. Results: www.dxy.com

Print (M.group (1))

#返回给定组1匹配的字符串. Results: DXY

Print (M.group (2))

#返回给定组2匹配的字符串. Result: com

8, start: The starting position of the given group match

Import RE

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

m = Pat.match (' www.dxy.com ')

Print (M.start (2)) #组2开始的索引, results: 8

9, end: The ending position of the given group match

Import RE

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

m = Pat.match (' www.dxy.com ')

Print (M.end (2)) #组2结束的索引. Results: 11

10. Span: The start end position of a given group match

Import RE

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

m = Pat.match (' www.dxy.com ')

Print (M.span (2))

The #组2的开始, ending index. The result is (8,11)

Python's 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.