我正在尝试使用 Jython 操作字符串,我在下面包含了一个示例字符串:
这将是网站的标题 :: SiteName
这将是网站的标题 :: SiteName :: SiteName
如何删除“:: Sitename”或“:: SiteName :: SiteName”的所有实例?
与常规 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 '
对于这样简单的示例,它是不必要的,但通常您可以使用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))