I have a list of strings that looks something like this:
list_strings = ["The", "11:2dog", "is", "2:33", "a22:11", "german", "shepherd.2:2"]
Here is what I want to do:
For each string in the list, I want to remove the numbers that match the pattern
number:number
. This pattern will always be at the beginning or the end of the string.When the pattern is removed from the string, I want to insert it as as the next element of the list if it is at the end, or as the previous element of the list if it is at the beginning of the string.
So:
list_strings = ["The", "11:2dog", "is", "2:33", "a22:11", "german", "shepherd.2:2"]
becomes:
new_list_strings = ["The", "11:2", "dog", "is", "2:33", "a", "22:11", "german", "shepherd.", "2:2"]
To find the words that may contain the pattern, I have tried using regular expressions:
for index, word in enumerate(list_strings):
try:
if re.search(r'\d+:\d+', word).group() != None:
words_with_pattern.append([index], word)
except:
pass
However, this only finds instances where the pattern is alone like "11:21". Once I have a list of all the words with the pattern, I will have to remove the pattern from the strings, note whether it is at the beginning or at the end, and insert it at the corresponding index in the list.
Any help? Thanks!