import re
regex = re.compile(r".*?($|\$[sc]\d)", flags=re.MULTILINE)
test_str = (" Есть текст и много строк\n"
"В тексте встречаются $s1 $s2 $s3 $s4 $c1 $c2 $c4\n"
"В каждой строке есть хоть 1 такой текст, могут быть и 2 и 3 в 1 строке, но без дублей.\n\n"
"Найти я смог их так (\\$s\\d)|(\\$c\\d) и удалить могу их всех, но как удалить все кроме них не понял.\n"
"Заранее спасибо за ответ! ")
subst = "$1"
result = regex.sub(subst, test_str)
if result:
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html