Skip to content

Instantly share code, notes, and snippets.

@Liam0205
Created October 18, 2017 05:03
Show Gist options
  • Select an option

  • Save Liam0205/330988e166d282b4c24cc028704ed440 to your computer and use it in GitHub Desktop.

Select an option

Save Liam0205/330988e166d282b4c24cc028704ed440 to your computer and use it in GitHub Desktop.
re.sub(http -> https)
import re
pattern = r'(?P<front><img.*?src=\\")(?P<protocol>http://)(?P<url>.*?\\")(?P<back>.*?\/>)'
_replace_protocol = lambda m: \
'%s%s%s%s' % (m.group('front'), 'https://', m.group('url'), m.group('back'))
source = r'<img src=\"http://gss1.bdstatic.com/-vo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D220/sign=ce3bec3b4d36acaf5de091fe4cd88d03/dc54564e9258d109b60a06b0d758ccbf6d814dc2.jpg\" layout=\"1\" style=\"float:right;\" class=\"fadeInLeftBig\" data-type=\"img\" />'
print source
print re.sub(pattern, _replace_protocol, source)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment