0

我正在尝试使用 Jython 操作字符串,我在下面包含了一个示例字符串:

这将是网站的标题 :: SiteName
这将是网站的标题 :: SiteName :: SiteName

如何删除“:: Sitename”或“:: SiteName :: SiteName”的所有实例?

4

2 回答 2

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 '
于 2008-10-23T15:40:19.047 回答
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))
于 2008-11-03T18:15:14.303 回答