D
D
DarkWood2016-09-25 10:36:13
Python
DarkWood, 2016-09-25 10:36:13

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()

As you can see, it takes the first value of each tuple from the list as a pattern for re.sub and the second value as a substitute for it.
First question: how to number the groups in the wildcards?
Second question: is it possible to use compilation flags (re.compile) or compilation itself? If possible, how?
Why do I have a second question? The replacement values ​​in the second line were originally written as independent ones and looked like this:
section_replace = re.compile(r'(^\d+.*$)', re.MULTILINE)
section_result = section_replace.sub(r'[COLOR=orange][SIZE=7]\1[/SIZE][/COLOR]', text)

That is, the compilation flag is used. Maybe you can change the search expression to something else without the flag? It searches for section headings (in bold):
1. Item title
Text paragraph
2. Second item title
Text paragraph
, etc.
In fact, the code shown above currently only works if the second line is removed from the list of replacements
. Please tell me the way to fix it. Just in case, I will say that additions to the list of replacements are possible in the future (and I'm not sure if they will use compilation flags, if this is important).
PS I don't know Python just a little less than at all. Solution taken from here .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lcd1232, 2016-09-28
@DarkWood

def add():
        result = text
        for pattern, replacement in SUBS:
                result = re.sub(pattern, replacement, result)
        return result

D
DarkWood, 2016-09-25
@DarkWood

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()

But now the problem is that only the last line in the list of replacements (in this case section_replace) is written to the file.
How to record all substitutions?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question