我有下一個問題。代碼完美地抓住了標籤,但是......我怎麼能把第二個「同時」的通道完成?IndexError:字符串索引超出範圍:多一次
如果我把這個道:
hash = '#hhhh #asdasd'
代碼不需額外編譯。我該如何改變這一段?預先感謝。 例外:
while hash[indexA + 1] not in {' ', '#'}:
IndexError: string index out of range
下面是代碼:
hash = '#hhhh #asdasd '
indexA = 0
copy = ''
indexB = indexA
while indexA < len(hash):
indexB = indexA
copy = ''
if hash[indexA] == '#' and indexA + 1 < len(hash):
while hash[indexA + 1] not in {' ', '#'}:
indexA += 1
copy = hash[indexB:indexA + 1]
if len(copy) > 1:
print('newHashtag: ' + copy)
if hash[indexA + 1] == ' ':
indexA += 1
else:
indexA += 1
'while indexA
Li357
此代碼在我的Python解釋器中工作。順便說一句,'hash'是Python中的一個內置函數,覆蓋它不是一個好主意。 – pivanchy
如果我明白你在做什麼,你應該真正使用這個正則表達式。如果你的字符串永遠是正常的,你甚至可以使用'split'。 –