As a regular segmented character string output [ 'info' , 'Xiaozhang' , '33 ' ,' shandong '], S = "info : xiaoZhang33shandong" ,
Import Re S = " info: Xiaozhang 33 is shandong " RES = re.split (R & lt " : | " , S) # | represents or, according to a colon or space division Geqie Print (RES) [ ' info ' , ' Xiaozhang ' , ' 33 is ' , ' shandong ' ]
Regular match ending 163.com mailbox
Import Re EMAIL_LIST = [ " [email protected] " , " [email protected] " , " [email protected] " ] for In Email in EMAIL_LIST: RET = re.match ( " [\ W] {4,20} 163.com $ @ " , email) IF RET: Print ( " % S is consistent with the provisions in line with the e-mail address:% S " % (email, ret.group ())) the else : Print ( " % S does not meet the requirements . " % Email) xiaowang @ -mail address 163.com is consistent with the provisions of the accord: xiaowang @ 163 .com xiaowang @ 163 .comheihei not meet the requirements [email protected] not meet the requirements
String a = "not404found Zhang Shenzhen 99", each word is a space intermediate, with a regular alphanumeric filtered off, the final output "San Shenzhen"
Using regular expressions to match the <html> <h1 \> www.baidu.com </ h1> </ html> the address (2) a = "Zhang points 98", with the re.sub, 98 will be replaced 100
import re source="<html><h1>www.baidu.com</h1></html>" pat=re.compile("<html><h1>(.*?)</h1></html>") print(pat.findall(source)[0]) s="张明98分" print(re.sub(r"\d+","100",s))
Regular expression matching (.) And (.?) To match the difference?
A :(.) Is very greedy mode may be more of a match, (.?), Also known as non-greedy lazy mode, generally matching the results just fine, matching characters less based, sample code as follows
Import Re S = " <HTML> <div> text 1 </ div> <div> Text 2 </ div> </ HTML> " PAT1 = the re.compile (R & lt " \ <div> (. *?) \ < / div> " ) Print (pat1.findall (S)) PAT2 = the re.compile (R & lt " \ <div> (*.) \ </ div> " ) Print (pat2.findall (S)) # output: [ 'text 1', 'text 2']; [ 'text 1 </ div> <div> text 2']
<divclass = "nam"> China </ div> , with a regular tag match in the content of the surface ( "China"), wherein the class name of the class is not determined