Compile a regex from the partial strings. Use re.escape()
in case they contain control characters in the regex language.
import re
date_parts = ['/Year', '/Month', '/Day']
pattern = re.compile('|'.join(re.escape(s) for s in date_parts))
Then use re.search()
to see if it matches.
string1 = "Tag01/Source 01/Start/Year"
re.search(pattern, string1)
The regex engine is probably faster than a native Python loop.
For your particular use case, consider concatenating all the strings, like
all_string = '\n'.join(strings+[''])
Then you can do them all at once in a single call to the regex engine.
pattern = '|'.join(f'.*{re.escape(s)}.*\n' for s in date_parts)
strings = re.sub(pattern, '', all_string).split('\n')[:-1]
Of course, this assumes that none of your strings has a '\n'
. You could pick some other character that's not in your strings to join and split on if necessary. '\f'
, for example, should be pretty rare. Here's how you might do it with '@'
.
all_string = '@'.join(strings+[''])
pattern = '|'.join(f'[^@]*{re.escape(s)}[^@]*@' for s in date_parts)
strings = re.sub(pattern, '', all_string).split('@')[:-1]
If that's still not fast enough, you could try a faster regex engine, like rure.