首页 > 解决方案 > What is the way of adding a word to the line below using regular expression in python?

问题描述

I need a regular expression in python to add {\an8} in the start of every line below the lines contains the two words 'position:50.00%' and 'line:10.00%' at the same time, in a format like this one, thanks.

file_content = re.sub(r'line:10.00%[^\n]*(\n*)([\w\W .,:%])', r'\1{\\an8}\2', file_content) 

Like this example:

00:05:31.957 --> 00:05:34.835  position:50.00%,start  align:start size:25.24%  line:10.00%
{\an8}Not yet.

标签: pythonregex

解决方案


不需要正则表达式。

previous_line_matched = False

for line in input:
    if previous_line_matched:
        print '{\\an8}' + line
    else:
        print line

    if 'position:50.00%' in line and 'line:10.00%' in line:
        previous_line_matched = True
    else:
        previous_line_matched = False

推荐阅读