Python re regular expression basic usage introduction

Source: Internet
Author: User

What is a regular expression?

Regular expressions provide the basis for advanced text pattern matching, extraction, and/or text-based search and replace functionality.

In simple terms, regular expressions (Regular expression, or regex) are strings of characters and special symbols that describe the repetition of patterns or the expression of multiple characters, and regular expressions can match a series of strings with similar characteristics according to a pattern.


Python re module provides regular functionality

Here are some basic uses:

#正则表达式元字符

1). (wildcard) can be used in addition to line breaks to refer to a

#ret = Re.findall (' W.. L ', s) #print ret

2) ^ Start matching (starting from the beginning)

#ret = Re.findall (' ^h...o ', ' Hejaodflhello ') #print (ret)

3) $ from end match

# ret = Re.findall (' A.. x$ ', ' afjdlajfa faf Alox ') # PRINT RET

4) * Repeat match (* pre-character [0 positive infinity])

# ret = Re.findall (' ab* ', ' Falfa;ldfafbaalexlil ') # print (ret)

5) + Match repeat [1, Infinity]

# ret = Re.findall (' ab+ ', ' Falfa;ldfabfbaabbleab2xlil ') # print (ret) # ret = Re.findall (' a+b ', ' Falfa; Ldfabfbaabbleab2xla2bil ') # PRINT RET

6)? [0,1] Times

# ret = Re.findall (' a?b ', ' Falfa;ldfabfbaabblea3b2xlil ') # PRINT RET

7) {} Specifies the number of occurrences of the preceding character

# ret = Re.findall (' a{5}b ', "ABXKAAAAAB,ABBBBBAAABBLFJDASSSSSBWL") # print ret# ret = re.findall (' a{1,3}b ', "Abxkaaab, Abbbbbabblfjdaabwl ") # PRINT RET

8) Character set and range matching []

#或关系选取

# ret = Re.findall (' a[c,d]x ', ' ACXBAADX ') # PRINT RET

Range selection

[0-9] equivalent to \d matches any decimal digit \d matches any non-numeric number in the opposite direction

[A-z] match small and letter

[A-z] matches uppercase letters

[a-za-z0-9] is equivalent to \w matches any alphanumeric and character \w to the contrary

\s matches any empty characters equivalent to [\n\t\r\v\f] \s the opposite



# ret = Re.findall (' [A-z] ', ' abcdef ') # PRINT RET

#[] Except for three cases where the special function of the meta-character is removed (\ ^-)

# ret = Re.findall (' [w,*,.,,] ', ' wa,x.dx* ') # print ret# take digit case Letter # ret = Re.findall (' [0-9a-za-z] ', ' 123tsAD ') # print ret# Result: [' 1 ', ' 2 ', ' 3 ', ' t ', ' s ', ' A ', ' D ']

Summarize:
* Equivalent to {0, infinity} is also equivalent to {0,}
+ equivalent to {1, infinity} is also equivalent to {1,}
? Equivalent to {0,1}


Take counter

# ret = Re.findall (' ^t ', ' T12iu3tsad ') #正常的取t开头的字符t # print ret# ret = Re.findall (' [^t,3] ', ' T12iu3tsad ') #取除t或3外的字符 # PR int ret# Result: [' 1 ', ' 2 ', ' I ', ' u ', ' s ', ' A ', ' D ']

\ back slash function
#功能有二:
#反斜杠后边跟随元字符去除特殊功能
#反斜杠后跟部分普通字符实现特殊功能


#\b matches any special character boundary of the boundary

# Print (Re.findall (R ' i\b ', ' hello,i am a li$st.i, ')) #结果: [' i ', ' I ', ' I ']# print (Re.findall (R ' \bi ', ' Hello Iam a Li$sti, ')) # Result: [' I ']

The common methods of RE

1. Re.match method

Matches only at the beginning of the string, and also returns the first object that matches to the return

Otherwise, none is returned

obj = Re.compile ("\.com") ret = Obj.match ("Print ret #结果None


2. Re.search method and Re.findall
#re. Search () returns the object with the first condition, and obtains the matching result by the Group method

#re. FindAll () put all the matching results into the list.

# ret = Re.search (' (? P<ID>\D{3})/(? P<name>\w{3}) ', ' WEEEW34TTTT123/PPPP ') # Print (Ret.group ()) #结果: 123/ppp# print (Ret.group (' id ')) #结果: 123# print ( Ret.group (' name ')) #结果 PPP
#ret = Re.findall (' (? P<ID>\D{3})/(? P<name>\w{3}) ', ' WEEEW34TTTT123/PPPP ') # PRINT ret #结果是所符合的存在于列表中: [(' 123 ', ' PPP ')]

3, Re.compile first compiled into matching objects can be called multiple times by other methods

obj = Re.compile (' \.com ') ret = Obj.findall ("www.baidu.com,https://www.google.com.cn") print ret #结果 ['. com ', '. com ']ret = Obj.search (www.baidu.com,https://www.google.com.cn). Group () print RET #结果. com

This article simply explains the basic application of regular expressions. For more extended usage please refer here. Https://deerchao.net/tutorials/regex/regex.htm

This article is from the "Learning, learning" blog, please be sure to keep this source http://dyc2005.blog.51cto.com/270872/1981625

Python re regular expression basic usage introduction

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.