我有一些示例字符串。我如何用空字符串替換長字符串中的第一次出現的字符串?替換Python中第一次出現的字符串
regex = re.compile('text')
match = regex.match(url)
if match:
url = url.replace(regex, '')
我有一些示例字符串。我如何用空字符串替換長字符串中的第一次出現的字符串?替換Python中第一次出現的字符串
regex = re.compile('text')
match = regex.match(url)
if match:
url = url.replace(regex, '')
字符串replace()功能如果完全解決了這個問題:
string.replace(s,old,new [,maxreplace])
返回字符串s的一個副本,其中所有出現的子字符串old都被new替換。如果給出可選參數maxreplace,則會替換第一個maxreplace事件。
>>> u'longlongTESTstringTEST'.replace('TEST', '?', 1)
u'longlong?stringTEST'
你在搜索和替換什麼?那個`url`是什麼? – BoltClock 2011-01-07 17:54:38