regular expressions python in basis and precautions

Feeling regular match is a cool thing, used judiciously can greatly improve programming efficiency. Although BeautifulSoup better use some in html, but sometimes still need to use a regular match. So this notes some of the learning and use of the process.

There comes python regular library: re. Use only need to import re just fine.

First sporadically write, to do after finishing:

re.compile (str expression) - returns a query by regular expression regular expressions can give an indication of the type of a variable assignment after assignment multiplex here this statement to a variable

a.search (str targetString) - with the search query method above regular classes to match the target text targetString, but only returns the first match successful outcome. General only as a detector expression is correct or not.

list a.findall (str targetString) - similar to the search, but as the name suggests you can get all the results

There are basically learning enough to use this site:

Regular expression syntax: http: //tool.oschina.net/uploads/apidocs/jquery/regexp.html

Written expression using a simple test for the website: http: //tool.oschina.net/regex/

 

Precautions:

Reference article here: https: //www.cnblogs.com/achillis/p/10353963.html

If we can get the desired result a.search match, we have to believe their expression is no problem. At this time, it is natural to think of all the results into findall, but sometimes get fragmented findall method looks wrong results.

This is because the regular findall will bracketed expressions matches the output portion alone, thus only some of the fragments obtained. To get the correct result, only the beginning of all end in their expression in parentheses plus:? Can.

 

Guess you like

Origin www.cnblogs.com/lingchuL/p/11441063.html