亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

python正則表達(dá)式如何在匹配到元素之后進(jìn)行處理再將處理結(jié)果替換原文?
高洛峰
高洛峰 2016-11-10 11:15:27
0
2
649

如要處理以下markdown:

# I miss you
my name is Gina,
This is my cloum: [sdf
df](http://zhuanlan.zhihu.com/xxxzzz)
and this is my weibo:[sina blog](https://link.zhihu.com/?target=http%3A//weibo.com/u/2355944%3Fis_hot%3D1)

我寫的代碼:

from __future__ import unicode_literals, print_function
import re
content='''
# I miss you
my name is Gina,
This is my cloum: [sdf
df](http://zhuanlan.zhihu.com/xxxzzz)
and this is my weibo:[sina blog](https://link.zhihu.com/?target=http%3A//weibo.com/u/2355944%3Fis_hot%3D1)
'''
linklist = re.findall('\[(.+?)\]\((.+?)\)',content,re.S)
for link in linklist:
    link[0].replace("\r\n","")
    link[1].replace("\r\n","")
    link[1].replace('https://ref.zhihu.com/?redrict=','')
print (content)

顯而易見的。。以上代碼不能運(yùn)行。。如何得到我想要的:

# I miss you
my name is Gina,
This is my cloum: [sdfdf](http://zhuanlan.zhihu.com/xxxzzz)
and this is my weibo:[sina blog](http://weibo.com/u/2355944?is_hot=1)


高洛峰
高洛峰

擁有18年軟件開發(fā)和IT教學(xué)經(jīng)驗(yàn)。曾任多家上市公司技術(shù)總監(jiān)、架構(gòu)師、項(xiàng)目經(jīng)理、高級軟件工程師等職務(wù)。 網(wǎng)絡(luò)人氣名人講師,...

全部回覆(2)
學(xué)霸

直接replace就可以了~

python3

>>>?content='''
#?I?miss?you
my?name?is?Gina,
This?is?my?cloum:?[sdfdf](http://zhuanlan.zhihu.com/xxxzzz)
and?this?is?my?weibo:[sina?blog](https://link.zhihu.com/?target=http%3A//weibo.com/u/2355944%3Fis_hot%3D1)
'''
>>>?x=content.replace(r'https://link.zhihu.com/?target=http%3A','http:')
>>>?print(x)

#?I?miss?you
my?name?is?Gina,
This?is?my?cloum:?[sdfdf](http://zhuanlan.zhihu.com/xxxzzz)
and?this?is?my?weibo:[sina?blog](http://weibo.com/u/2355944%3Fis_hot%3D1)


三叔
from?__future__?import?unicode_literals,?print_function
from?urllib?import?unquote
import?os,?html2text,?re

content='''
#?I?miss?you
my?name?is?Gina,
This?is?my?cloum:[sdf
df](http://zhuanlan.zhihu.com/xxxzzz)
and?this?is?my?weibo:[sina?blog](https://link.zhihu.com/?target=http%3A//weibo.com/u/2355944%3Fis_hot%3D1)
'''

def?ZhiHu_LinkParser(content):
????return?re.sub('(\[.+?\])(\(.+?\))',?LinkReplace,?content,0,re.S)

def?LinkReplace(matched):
????return?RRN(matched.group(1))?+?unquote(RRN(matched.group(2)).replace('https://link.zhihu.com/?target=',''))

def?RRN(str):
????return?re.sub(r'\r*\n*',"",str)

print?(ZhiHu_LinkParser(content))


最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板