程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Re module in python (1)

編輯:Python

1. compile() function

Compiling regular expression patterns , Returns the pattern of an object ( You can compile those commonly used regular expressions into regular expression objects , It's a little bit more efficient ).

Format :

re.compile(pattern,flags=0)

for example :

import re
text = "The moment you think about giving up,think of the reason why you held on so long."
text1 = "Life is a journey,not the destination,but the scenery along the should be and the mood at the view."
rr = re.compile(r'\w*o\w*')
print(rr.findall(text)) # lookup text All contained in 'o' 's words 
print(rr.findall(text1)) # lookup text1 All contained in 'o' 's words 

The operation results are as follows :

['moment', 'you', 'about', 'of', 'reason', 'you', 'on', 'so', 'long']
['journey', 'not', 'destination', 'along', 'should', 'mood']
  • pattern: Expression string used at compile time ( Regular expressions );
  • flags:( Optional ) Compile flag bit , Used to modify the matching method of regular expressions , Such as : Is it case sensitive , Multiline matching, etc .

frequently-used flags Yes :

sign meaning re.S(DOTALL) send . Match all characters including line breaks re.I(IGNORECASE) Make match match case insensitive re.L(LOCALE) Do localization identification (locale-aware) matching , French, etc re.M(MULTILINE) Multi-line matching , influence ^ and $re.X(VERBOSE) This flag is written in a more flexible format to make regular expressions easier to understand re.U according to Unicode Character set parsing characters , This sign affects \w,\W,\b,\B

2. match() function

Match at the beginning of the string , Matches the destination character at the beginning and returns , If there is no destination character at the beginning, the matching will fail , return None.

Format :

re.match(pattern, string, flags=0)

for example :

print(re.match('edu','educoder.net').group())
print(re.match('edu','www.educoder.net').group())

The operation results are as follows :

notes :match() The function returns a match object object , and match object The object has the following methods :

  • group(): Returns the regular matching string ;
  • start(): Returns the position where the match started ;
  • end(): Returns the position where the match ends ;
  • span(): Returns a tuple containing a match ( Start , end ) The location of ;
  • groups(): Returns the regular whole matching string , You can enter more than one group number at a time , The string matching the corresponding group number .

3. search() function

re.search() Function to find a pattern match in a string , Just find the first match and go back . If the string doesn't match , Then return to None.

Format :

re.search(pattern, string, flags=0)
print(re.search('edu','www.educoderedu.net').group())
print(re.search('eduaaa','www.educoderedu.net').group())

The operation results are as follows :

  • notes :match() and search() similar , The difference is match() Match only the beginning of the string , If the destination string does not appear at the beginning , Even if it appears later, it will not be matched ;search() Function will match within the whole character , As long as a destination string is found, it returns .

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved