2008-10-23 70 views
0

我試圖操縱用Jython字符串,我已經包含下面的示例串:Jython的劈裂串起

這將是一個網站::網站名稱
標題這將是一個網站的標題:: SiteName :: SiteName

我想刪除「:: Sitename」或「:: SiteName :: SiteName」的所有實例,任何人都可以幫我嗎?乾杯

回答

2

從常規的Python沒有什麼不同:

>>> str="This would be a title for a website :: SiteName" 
>>> str.replace(":: SiteName","") 
'This would be a title for a website ' 
>>> str="This would be a title for a website :: SiteName :: SiteName" 
>>> str.replace(":: SiteName","") 
'This would be a title for a website ' 
0

對於這樣簡單的例子,這是不必要的,但一般而言,您可以使用re模塊。

import re 

sitename = "sitename" #NOTE: case-insensitive 
for s in ("This would be a title for a website :: SiteName :: SiteName", 
      "This would be a title for a website :: SiteName"): 
    print(re.sub(r"(?i)\s*::\s*%s\s*" % sitename, "", s))