2012-01-02 75 views
23

我已经从论坛中提取了一些信息。它是原始字符串我现在有:使用Python删除子字符串

string = 'i think mabe 124 + <font color="black"><font face="Times New Roman">but I don\'t have a big experience it just how I see it in my eyes <font color="green"><font face="Arial">fun stuff' 

我不喜欢的东西是子串"<font color="black"><font face="Times New Roman">""<font color="green"><font face="Arial">"。除了这个之外,我想保留字符串的其他部分。所以结果应该是这样的

resultString = "i think mabe 124 + but I don't have a big experience it just how I see it in my eyes fun stuff" 

我该怎么做?其实我用美丽的汤从论坛中提取上面的字符串。现在我可能更喜欢使用正则表达式来删除部分。

+0

此字符串目前没有工作,它既有'“'和'''里面 – juliomalegria 2012-01-02 16:23:40

+0

@ThiefMaster谢谢支持,我怎么能删除吗?这是肯定的 – 2012-01-02 16:23:41

+0

@ julio.alegria一种耻辱请刚如果你想进行一些测试,请把开头和结尾之间的东西当作一个字符串来处理。谢谢lot – 2012-01-02 16:24:39

回答

53
import re 
re.sub('<.*?>', '', string) 
"i think mabe 124 + but I don't have a big experience it just how I see it in my eyes fun stuff" 

re.sub功能需要一个正规表示法,并更换所有的比赛与第二个参数中的字符串中。在这种情况下,我们正在搜索所有标签('<.*?>')并将其替换为无('')。

?用于re用于非贪婪搜索。

更多关于re module

+8

你是我的英雄 – 2012-01-02 16:33:25

+0

@ Wenhao.SHE很高兴我帮助:) – juliomalegria 2012-01-02 16:35:45

+0

这是非常有用的..谢谢,我用这个来删除Twitter中的提及(@s)tweets我的项目 - re.sub('@。* ?','',tweetText) – sumanth232 2015-03-27 09:41:25

10
>>> import re 
>>> st = " i think mabe 124 + <font color=\"black\"><font face=\"Times New Roman\">but I don't have a big experience it just how I see it in my eyes <font color=\"green\"><font face=\"Arial\">fun stuff" 
>>> re.sub("<.*?>","",st) 
" i think mabe 124 + but I don't have a big experience it just how I see it in my eyes fun stuff" 
>>> 
+7

你也很棒 – 2012-01-04 13:59:24