Skip to content

Instantly share code, notes, and snippets.

@kenneth
Created May 23, 2013 07:46
Show Gist options
  • Save kenneth/5633351 to your computer and use it in GitHub Desktop.
Save kenneth/5633351 to your computer and use it in GitHub Desktop.
a newbie regex problem?
# -*- coding:utf-8 -*-
#!/usr/bin/env python
import re
content="""
[URL="http://www.boston.com/bigpicture/2008/10/the_sun.html"]http://www.boston.com/bigpictu....html[/URL]
[url="http://www.boston.com/bigpicture/2008/10/the_sun.html"]http://www.boston.com/bigpictu....html[/url]
[URL=http://www.boston.com/bigpicture/2008/10/the_sun.html]http://www.boston.com/bigpictu....html[/URL]
[url=http://www.boston.com/bigpicture/2008/10/the_sun.html]http://www.boston.com/bigpictu....html[/url]
[url]http://www.boston.com/bigpictu....html[/url]
[URL]http://www.boston.com/bigpictu....html[/URL]
"""
pattern = r'(?i)\[url="?[\s\S]*?\]'
for match in re.finditer(pattern, content):
print match.group()
@SeavantUUz
Copy link

能在这里写么?我想可能应该这样

import re
content="""
[URL="http://www.boston.com/bigpicture/2008/10/the_sun.html"]http://www.boston.com/bigpictu....html[/URL]
[url="http://www.boston.com/bigpicture/2008/10/the_sun.html"]http://www.boston.com/bigpictu....html[/url]
[URL=http://www.boston.com/bigpicture/2008/10/the_sun.html]http://www.boston.com/bigpictu....html[/URL]
[url=http://www.boston.com/bigpicture/2008/10/the_sun.html]http://www.boston.com/bigpictu....html[/url]
[url]http://www.boston.com/bigpictu....html[/url]
[URL]http://www.boston.com/bigpictu....html[/URL]
"""
pattern = r'.*(http://.*html?).*'
pat = re.compile(pattern)
pat.findall(content)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment