Tag: python regular expression
#python正则表达式import Reif __name__== "__main__": subpattern= "(([a-za-z]+) \.) * "Subpattern2=" ([a-za-z]+) "pattern="%s%[email protected]%s%s "% (Subpattern, subPattern2, Subpattern,subpat TERN2) print (pattern) mail1= "[email protected]" mail2= "[email protected]" Rc=re.compile (Patt ERN) m1 = Rc.match (MAIL1) if M1:print ("m1=%s"% (M1.groups (),)) Print ("M1.group () =%s"% (M1.group (0), )) m2= Rc.match (MAIL2) if M2:print ("m2=%s"% (M2.groups (),)) Print ("M2.group () =%s"% (m2.group (0) ,)) mail3= "<tom paris> [email protected]" M3=re.match ("^< ([a-za-z\\s]+) >\\s*[a-za-z ][email protected][a-za-z]+\\. [a-za-z]+ ", Mail3) assert (m3! = None," Match failed!!! ") Print ("m3=%s"% (m3.groups ()) Print ("M3.group (0) =%s"% (M3.group (0),)) Print ("M3.group (1) =%s"% (M3.group (1),)) "" "#输出结果 (([a-za-z]+) \.) * ([a-za-z]+) @ (([a-za-z]+) \.) * ([a-za-z]+) m1= (None, NonE, ' Someone ', ' Gmail. ', ' gmail ', ' com ') m1.group () [email protected] m2= (' Bill ', ' Bill ', ' Gates ', ' Microsoft. ') , ' Microsoft ', ' com ') m2.group () [email protected] m3= (' Tom Paris ',) m3.group (0) =<tom paris> [Email&nbs P;protected] M3.group (1) =tom Paris "" "
Python.re Module