Using regular expressions in python

while doing asian beauty pictures

I have learned more modes in the front, including forward search, backward search, positive mode, and negative mode. This time I will learn another one, which is the backward search positive mode, which means that the string that has been scanned, and I want to regret it to see if it can be matched. Its syntax is: (?<=pattern). For example, the following example is used to identify Twitter accounts, but this pattern will only match and will not appear in the matching string, as follows:
 

[python]  view plain copy  
 
 
  1. #python 3.6  
  2. #Cai Junsheng   
  3. # http://www.meimei689.com 
  4. #  
  5. import re  
  6.   
  7. twitter = re.compile(  
  8.     ''''' 
  9.     # A twitter handle: @username 
  10.     (?<=@) 
  11.     ([\w\d_]+)       # username 
  12.     ''',  
  13.     re.VERBOSE)  
  14.   
  15. text = '''''This text includes two Twitter handles. 
  16. One for @caimouse, and one for the author, @caijunsheng. 
  17. '''  
  18.   
  19. print(text)  
  20. for match in twitter.findall(text):  
  21.     print('Handle:', match)  



 The resulting output is as follows:
 This text includes two Twitter handles.
One for @caimouse, and one for the author, @caijunsheng.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326495045&siteId=291194637