Bitopan Gogoi
Bitopan Gogoi

Reputation: 125

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

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.

Upvotes: 0

Views: 21

Answers (1)

Amin
Amin

Reputation: 2863

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

Upvotes: 1

Related Questions