0

In below code, what does each and every element of the symbol string re.sub('<[^>]*>|[\n]|\[[0-9]*\]', '', htmlread) mean?

import urllib2
import re

htmltext = urllib2.urlopen("https://en.wikipedia.org/wiki/Linkin_Park")
htmlread = htmltext.read()
htmlread = re.sub('<[^>]*>|[\n]|\[[0-9]*\]', '', htmlread)
regex = '(?<=Linkin Park was founded)(.*)(?=the following year.)'
pattern = re.compile(regex)
htmlread = re.findall(pattern, htmlread)
print "Linkin Park was founded" + htmlread[0] + "the following year."
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120

2 Answers2

0

The line htmlread = re.sub('<[^>]*>|[\n]|\[[0-9]*\]', '', htmlread) removes either

  • an expression between <> OR
  • a newline
  • a number between brackets or empty brackets

from htmlread

interesting wiki post here: Reference - What does this regex mean?

Community
  • 1
  • 1
Jean-François Fabre
  • 126,787
  • 22
  • 103
  • 165
0

Replace every character with '', that means delete it from htmlread variable

Please read more about RegEx