這是我的代碼:有什麼錯我的Python應用re.sub
string ='''
{% emoji 'MONEY_BAG' %}<span style="color:#7F6C41;"><a href="{% mobile_url '/inventory/view_item/?category=weapon&inventory_id=%s' inventory_id %}">{{ item.name }}</a>を入手した!</span></span>
'''
a = r'''
{%\s+mobile_url\s+['"]{1}(/inventory/view_item/\?)[^'"]*['"]{1}\s+([^%}]+)\s+%}
'''
def aa(x):
print x.group(1)
print x.group(2)
return ''
string = re.sub(a, aa, string)
print string
,它顯示:
{% emoji 'MONEY_BAG' %}<span style="color:#7F6C41;"><a href="{% mobile_url '/inventory/view_item/?category=weapon&inventory_id=%s' inventory_id %}">{{ item.name }}</a>を入手した!</span></span>
我要打印的x.group(1)和x.group (2)
所以我能做些什麼,
感謝
您能否提供一個預期產出的例子?另外,是否有可能你正在[用正則表達式解析html](http://stackoverflow.com/questions/1732348/)? – 2011-06-17 07:05:04
它不打印任何東西,因爲再也找不到匹配 – 2011-06-17 07:06:05
您是否試圖用正則表達式解析HTML? – 2011-06-17 07:11:52