The Python regular expression re FindAll method can return a matching substring in the form of a list.
Re.findall (pattern, string[, flags]):
Searches for a string, returning all matching substrings as a list. Let's look at a simple code:
Import re
p = re.compile (R ' \d+ ')
Print P.findall (' One1two2three3four4 ')
# # # output # #
# [' 1 ', ' 2 ', ' 3 ', ' 4 ']
Slightly more complex, such as:
info = ' <a href= ' http://www.baidu.com >baidu</a> ' Our need is to extract URLs and anchor text through regular expressions, which can be used
FindAll ()
Import re
relink = ' <a href= ' (. *) "> (. *) </a> '
info = ' <a href= ' http://www.baidu.com >baidu</a> '
Cinfo = Re.findall (relink,info)
Print Cinfo
The result of the output: [(' http://www.baidu.com ', ' Baidu ')] Returns a list that contains the form of a tuple that matches the result. If you need to replace it with regular, you can look at the Python re Sub
Python Regular Expressions