----------the RE module for regular use----------
#result =re.match (regular expression, string to match): filter strings using regular to start matching from front
#result. Group (): Take out the obtained data
#result =re.search (regular expression, string to match): filter strings using regular to match from behind
#result ==none: Determines whether the regular expression gets to the content and, if true, does not get the content
#re. Search (R ' \d+ ', ' my hight 177 cm '). Group (): Filter using a regular read string to start a match by finding a character that meets the requirements.
#re. FindAll (R ' \d+ ', ' my hight 177 cm my weight kg '): Gets all the data information in the string that matches the regular condition and saves it to a list
#re. Sub (R ' \d+ ', ' + ', ' my high 177 cm '): Gets all the data information in a string that conforms to the regular condition and replaces it with data information from the second location
The second position can be processed with the function, return value is str type
#re. Split (R ': | ', ' address:beijing [email protected] ': effective cutting of strings based on rules provided by regular expressions. and store the results in the corresponding list
----------Regular Expression single-character matching----------
.: Matches any character
[]: Match the characters listed in []
\d: Match any number
\d: Matches a non-numeric, that is, not a number
\s; match empty bar that is space, TAB key
\s: matching non-whitespace
\w: Match word characters, letters, numbers, underscores
\w: Matches non-word characters, letters, numbers, underscores
----------a regular expression to match multiple characters----------
*: Matches the previous character 0 or unlimited times, that is, optional
+: Matches the previous character 1 or more times, that is, only 1 times
?: matches the previous character 1 or 0 times, either 1 times or no
{m}: match the previous character appears m times
{M,n}: Match the previous character appears from M to N times
---------Match start End----------
^: Match string start
$: Match string end
\: Escaped
---------Matching grouping----------
|: Match left and right expression
(): Enclose the characters in parentheses as a group
\<num>: String to which the reference group NUM matches
(? p<name>): Group up aliases
(? P=name): The string to which the reference alias matches the name group
---------attached: Regular expression exercises---------
1. Matching URLs
There are a number of Web sites:
Http://www.interoem.com/messageinfo.asp?id=35
Http://3995503.com/class/class09/news_show.asp?id=14
http://lib.wzmc.edu.cn/news/onews.asp?id=769
Http://www.zy-ls.com/alfx.asp?newsid=377&id=6
http://www.fincm.com/newslist.asp?id=415
The need for regular after is:
http://www.interoem.com/
http://3995503.com/
http://lib.wzmc.edu.cn/
http://www.zy-ls.com/
http://www.fincm.com/
Code implementation:
1 defTestfirst ():2 #the data to be processed3Strhtml='http://www.interoem.com/messageinfo.asp?id=35 http://3995503.com/class/class09/news_show.asp?id=14/http lib.wzmc.edu.cn/news/onews.asp?id=769 http://www.zy-ls.com/alfx.asp?newsid=377&id=6 http://www.fincm.com/ newslist.asp?id=415'4 5 #strhtml= ' http://www.interoem.com/messageinfo.asp?id=35 '6 7 8 Print("data before conversion:%s"%strhtml)9 Ten #The process is working on an expression OneResult=re.findall ("(http://.*?\. ( COM|CN)/)", strHTML) A - #Test - #result = Re.match ("Http://.*\." ( COM|CN)/", strhtml). Group () the - #Create a variable to store the results -strresult="' - + #variable Result - forIteminchResult: +strresult+=item[0]+" " A at #Print out Results - Print("corresponding data after conversion:%s"%strresult)Testone
2. Match all valid Python identifiers
Implementation code:
1 #referencing the corresponding package2 ImportRe3 4 Importkeyword5 6 #2. Match all valid Python identifiers7 deftestfive ():8 #get a list of keywords in Python9keylist=keyword.kwlistTen Onestrkey="("+'|'. Join (Keylist) +")" A - #get data to be processed -strtitle="int main str wfwfwfwfdsfstr Andand ifwhile" the - #Print the data you want to work with - Print("data before processing:%s"%strtitle) - + #do regular processing -result=Re.findall (strkey,strtitle) + A #Print processed Data at Print("processed data:%s"%STR (Result))Testtwo
3. Match a legitimate IP address
Code implementation:
1 Reference Package2 ImportRe3 4 #3. Match a legitimate IP address5 deftestsex ():6 #Accept the IP address entered by the user7Strtitle=raw_input ("Please enter the IP address to be judged:")8 9Strre="'Tenstrre+='([1-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])'#First place Onestrre+='\.' Astrre+='([1-9]?[ 0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'#second place -strre+='\.' -strre+='([1-9]?[ 0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'#third place thestrre+='\.' -strre+='([1-9]?[ 0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) $'#Fourth place - - #to judge whether IP is legitimate +result=Re.match (strre,strtitle) - + ifresult==None: A Print("Match failed! ") at Else: - Print("Match Success! ")Testthree
Python Regular Expression notes