[Python-de] Index im String suchen
Fritz Cizmarov
fritz at sol.at
Sun Feb 2 19:17:48 EST 2003
Hallo Albert,
für Dein Problem wirst Du wohl eine Funktion brauchen, mit den
regexpr-Funktionen von Python wird das wohl nicht gehen.
def findall(s, sub):
erg = []
i = 0
while i < len(s):
f = find(s, sub, i)
if f < 0: break
erg.append(f)
i = f+1
return erg
>>> a = "Das ist <bo> foo </bo> das ist <bo> bar </bo> das ist nichts"
>>> findall(a, "<bo>")
[8, 31]
>>> findall(a, "</bo>")
[17, 40]
damit bekommst Du alle Indices von "<bo>" und "</bo>" geliefert und
kannst damit machen was Du willst.
Gruß
Dookie
More information about the Python-de
mailing list