jeudi 26 mars 2015

Find and use multiple occurences of a string in a string

I recently started using Python and wrote some simple scripts Now I have this question:


I have this string:



mystring = 'AAAABBAAABBAAAACCAAAACCAAAA'


and I have these following strings:



String_A = BB
String_B = CC


I would like to get all possible combinations of strings starting with String_A and ending with String_B (kind of vague so below is the desired output)



output:
BBAAABBAAAACCAAACC
BBAAABBAAAACC
BBAAACCAAAACC
BBAAACC


I am able to count the number of occurences of String_A and String_B in mystring using



mystring.count()


And I am able to print out one specific output (the one with the first occurence of String_A and the first occurence of String_B), by doing the following:



if String_A in mystring:
String_B_End = mystring.index(String_B) + len(String_B)
output = mystring[mystring.index(String_A); String_B_End]
print(output)


this works perfect but only gives me the following output:



BBAAABBAAAACC


How can I get all the specified output strings from mystring? thanx in advance!


Aucun commentaire:

Enregistrer un commentaire