Answer the question
In order to leave comments, you need to log in
Python: Multiple replacement from list consisting of RegExp. How to use group numbering and compilation flags?
Hello.
At the moment we have the following code:
import re
SUBS = [
(r'(Шаг \d+)', r'[COLOR=orange][SIZE=7]\1[/SIZE][/COLOR]'),
(r'(^\d+.*$)', r'[COLOR=orange][SIZE=7]\1[/SIZE][/COLOR]'),
]
file = open('d:/text_sample.txt','r')
text = file.read()
def add():
for pattern, replacement in SUBS:
result = re.sub(pattern, replacement, text)
return result
file.close()
file1 = open('d:/text_sample.txt','w')
file1.write(add())
file1.close()
section_replace = re.compile(r'(^\d+.*$)', re.MULTILINE)
section_result = section_replace.sub(r'[COLOR=orange][SIZE=7]\1[/SIZE][/COLOR]', text)
Answer the question
In order to leave comments, you need to log in
def add():
result = text
for pattern, replacement in SUBS:
result = re.sub(pattern, replacement, result)
return result
It seems to have figured out the compilation. Changed the code like this:
import re
step_replace = re.compile(r'(Шаг \d+)')
section_replace = re.compile(r'(^\d.+$)', re.MULTILINE)
SUBS = [
(step_replace, r'[COLOR=orange][SIZE=7]\1[/SIZE][/COLOR]'),
(section_replace, r'[COLOR=orange][SIZE=7]\1[/SIZE][/COLOR]'),
]
file = open('d:/text_sample.txt','r')
text = file.read()
def add():
for pattern, replacement in SUBS:
result = re.sub(pattern, replacement, text)
return result
file.close()
file1 = open('d:/text_sample.txt','w')
file1.write(add())
file1.close()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question