Check occurrence of a string, a part of which might vary

Viewed 19

I am trying to search for a string "SAMPLEX_FIND", where abphabet in place of "X" might vary. For example- "ABCSAMPLEA_FINDER", "asrSAMPLEB_FINDer" etc.

1 Answers

You can use regex for that:

import re
example = "ABCSAMPLEA_FINDER"
print(re.findall(r".*SAMPLE._FIND.*", example)[0])

Note that if the function couldn't find the pattern, the result would be an empty list and the index won't work

Related