python - 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.
解决方案
不需要正则表达式。
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
推荐阅读
- java - 为什么 HTMLEditorKit 不想在元素内插入元素?
- python - Can the input for "Lasso" in python contain categorical variables?
- blazor - 使用 Blazor 验证时如何清除错误消息
- php - 如何在 PHP 中跟踪目录的索引
- git - How to check for not pushed changes to Gerrit
- php - Login system data validating from database
- java - 自定义模型的缓存/持久性的最佳方法
- python - When build with py2app can't save in folder
- java - Are there some common techniques for Java Web Servers to detect when approaching overload?
- oracle - 将 SQL Server 查询转换为 Oracle