I need find a repeated sequences of same my_string = 'ABCD'
in string my_data = 'ABCDABCDRRRABCD'
and return the longest one 'ABCDABCD'
.
If I use result = ([max(i) for i in re.findall(r'((ABCD)\2+)', data)])
It's works fine, but I can't put a variable my_string
so I can use it for difference strings in some loop.
Asked
Active
Viewed 104 times
-1

Oleg Kobeliatskyi
- 25
- 6
1 Answers
-1
To put variable inside of regular expression you can use .format
result = ([max(i) for i in re.findall(r'(({0})\2+)'.format(my_string), my_data)])

Oleg Kobeliatskyi
- 25
- 6
-
1typo? should be ```result = ([max(i) for i in re.findall(r'(({})\2+)'.format(my_string), my_data)])``` – pink spikyhairman May 03 '20 at 18:43
-
different type of placeholder, both style works fine – Oleg Kobeliatskyi May 03 '20 at 18:59
-
I did not know that :) – pink spikyhairman May 03 '20 at 19:04