python regular expression 實例
import re
from_url = 'http://www.juksy.com/index.php/private-property/item/17519-juksy-%E5%90%8D%E4%BA%BA%E7%A7%81%E7%89%A9-vol17%E5%A4%A7%E5%98%B4%E5%B7%B4%EF%BC%88da-mouth%EF%BC%89'
re_juksy = re.compile(r'(?P<prefix>(.)*)/item/(?P<artical_id>[\d]+)')
if 'juksy.com' in from_url:
match = re_juksy.findall(from_url)
if match:
from_url = '%s/item/%s' % (match[0][0], match[0][2])
效果:
from_url 從 http://www.juksy.com/index.php/private-property/item/17519-juksy-%E5%90%8D%E4%BA%BA%E7%A7%81%E7%89%A9-vol17%E5%A4%A7%E5%98%B4%E5%B7%B4%EF%BC%88da-mouth%EF%BC%89
變成
http://www.juksy.com/index.php/private-property/item/17519
from_url = 'http://www.juksy.com/index.php/private-property/item/17519-juksy-%E5%90%8D%E4%BA%BA%E7%A7%81%E7%89%A9-vol17%E5%A4%A7%E5%98%B4%E5%B7%B4%EF%BC%88da-mouth%EF%BC%89'
re_juksy = re.compile(r'(?P<prefix>(.)*)/item/(?P<artical_id>[\d]+)')
if 'juksy.com' in from_url:
match = re_juksy.findall(from_url)
if match:
from_url = '%s/item/%s' % (match[0][0], match[0][2])
效果:
from_url 從 http://www.juksy.com/index.php/private-property/item/17519-juksy-%E5%90%8D%E4%BA%BA%E7%A7%81%E7%89%A9-vol17%E5%A4%A7%E5%98%B4%E5%B7%B4%EF%BC%88da-mouth%EF%BC%89
變成
http://www.juksy.com/index.php/private-property/item/17519
留言
張貼留言