L
L
LakeForest2021-12-04 14:42:57
Python
LakeForest, 2021-12-04 14:42:57

How to write a regex in python so that in continuous text every sentence starting with "Operator:" wraps to the next line?

How to write a regular expression so that in a continuous text, each sentence starting with "ClientN :" wraps to the next line and erases the extra space?
For example:

text = '''
Оператор: Здравствуйте, чем могу помочь? Клиент : Есть идея: взял кредит, хочу погасить сразу весь. Возможно? Оператор:  Минуточку, сейчас посмотрим, что можно сделать.
'''

I want to save it to a file like this:
Оператор: Здравствуйте, чем могу помочь? 
Клиент: Есть идея: взял кредит, хочу погасить сразу весь. Возможно?
Оператор:  Минуточку, сейчас посмотрим, что можно сделать.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2021-12-04
@LakeForest

print(re.sub(r'(Оператор|Клиент)\s?:', r'\n\g<1>:', text).strip())

V
Vladimir Kuts, 2021-12-04
@fox_12

re.sub('Менеджер\s+:', '\nМенеджер:', yourtext)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question