Код: Выделить всё
list = ['word one', 'two', 'Three', 'four five' 'four']
Код: Выделить всё
txt_to_parse = "This is word one with four letters and four five characteristics for people #gopeople"
Код: Выделить всё
#gopeople (goup(0) - #, Group(1) - gopeople
four - position xy string match - 'four'
four - position yz string match - 'four'
four five - position yz string match - 'four five'
Есть ли лучший способ решить эту проблему, кроме создания список (List_loop) re.compile для каждого требования:
- хэштег или упоминание, за которым следуют слова
- re .компилировать для всех ключевых слов в списке
Примерно так:
Код: Выделить всё
fruit_list = ['apple banana', 'apple', 'pineapple', 'banana', 'banana apple', 'kiwi']
fruit = re.compile('|'.join(fruit_list))
fruit_re = [ re.compile(r'\b'+re.escape(fruit)+r'\b') for fruit in fruit_list]
fruit_re.append(re.compile( r'([#@])(\w+)|\b'))
string = "this is apple is banana apple #apple"
for ft in fruit_re:
print(ft)
match = re.finditer(ft, string)
for mat in match:
print(mat)
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/791 ... of-strings
Мобильная версия