how can I find the index of the second repetition of a string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how can I find the index of the second repetition of a string?

f = open("newFile.txt", "w+") pathName = os.path.abspath(f.name) #I need to find the index of the second repetition of the back slash

30th May 2020, 7:30 PM
Jeremy Cruz
Jeremy Cruz - avatar
6 Answers
30th May 2020, 7:53 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 2
Do you mean second instance, or second repetition (i.e. 3rd instance)? I would recommend regex for this. import re string = 'hello//world//ygc' print(re.match(r'(?:.*?/.*?/.*?)/', string).end() - 1) #12 3rd instance. print(re.match(r'(?:.*?/.*?)/', string).end() - 1) #6 2nd instance.
30th May 2020, 7:48 PM
Russ
Russ - avatar
+ 2
Kuba SiekierzyƄski Very nice! You win this regex round! 😉
30th May 2020, 7:56 PM
Russ
Russ - avatar
+ 1
Russ I used to love this game :) Reminds me of some very good time in my life ;)
30th May 2020, 8:00 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
0
the back slash is repeated about five times in the string and I want to find the index of the second backslash.
30th May 2020, 7:49 PM
Jeremy Cruz
Jeremy Cruz - avatar
0
I’m not really understanding. Specially the “(?:.*?/.*?)”
30th May 2020, 7:53 PM
Jeremy Cruz
Jeremy Cruz - avatar