鱼C论坛

 找回密码
 立即注册
查看: 1667|回复: 2

[已解决]单个正则,怎么合并srt字幕里的多行字幕(可能有1/2/...n行)为1行?

[复制链接]
发表于 2022-4-11 17:49:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 blackantt 于 2022-4-12 09:40 编辑

怎么合并srt字幕里的多行(行数不定)为1行?   如下,粗体行行数不定,最终想合成1行

10302
07:49:42,412 --> 07:49:45,874
you guys don't have to take
care of trina anymore tonight.


10303
07:49:45,916 --> 07:49:47,208
yeah, you do.

10304


%s/^M//g
%s/\(,\d\d\d\n.*\)\n\(.\+\)/\1 \2/g    只能2行合成1行,不定行数该咋搞呢?
%s/\(,\d\d\d\n.*\)\n\(.\+\)/\1 \2/g    如果还有2行的,再重复执行一下

或者用以下

ggvGJ  合并所有行
%s/\(\d\{1,100000\}\) \(\d\d:\d\d:\d\d,\d\d\d --> \d\d:\d\d:\d\d,\d\d\d\)/\r\r\1\r\2\r/g
删除第一行的空行

最佳答案
2022-4-11 19:23:04
本帖最后由 阿奇_o 于 2022-4-11 19:25 编辑

正则我是暂时写不出来的,给个看起来有点繁琐的办法吧,
  1. """ 注:以下代码,只适合 各段字幕都以空行作为分隔 """
  2. with open('zimu.txt', encoding='GB2312') as f:
  3.     lines = f.readlines()
  4.     # print(lines)
  5.     # print(lines.index('\n'))

  6.     tmp = lines[:] + ['\n']   # 复制一份(且保证最后一行为空行)
  7.     t = tmp.count('\n')   # 空行数
  8.     for i in range(t):
  9.         empty_pos = tmp.index('\n')   
  10.         # print(empty_pos)
  11.         # group = tmp[ : empty_pos ]
  12.         # print(tmp[empty_pos-3].strip().isdigit())
  13.         # 单行字幕的情况
  14.         if tmp[empty_pos-3].strip().isdigit():   
  15.             group = tmp[:empty_pos]
  16.             # print(group)
  17.             tmp = tmp[empty_pos+1:]
  18.             print('各段字幕:\n', ''.join(group)+'\n')
  19.         else:  
  20.             # 双行字幕的情况
  21.             group = tmp[:empty_pos-2] + [''.join(tmp[empty_pos-2:empty_pos]).replace('\n', ' ')]
  22.             # print(group)
  23.             tmp = tmp[empty_pos+1:]
  24.             print('各段字幕:\n', ''.join(group)+'\n')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-11 18:09:18 | 显示全部楼层
本帖最后由 blackantt 于 2022-4-11 19:03 编辑

%s/\(,\d\d\d\n.*\)\n\(.\+\)/\1\2/g   只能2行合成1行,不定行数该咋搞呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-11 19:23:04 | 显示全部楼层    本楼为最佳答案   
本帖最后由 阿奇_o 于 2022-4-11 19:25 编辑

正则我是暂时写不出来的,给个看起来有点繁琐的办法吧,
  1. """ 注:以下代码,只适合 各段字幕都以空行作为分隔 """
  2. with open('zimu.txt', encoding='GB2312') as f:
  3.     lines = f.readlines()
  4.     # print(lines)
  5.     # print(lines.index('\n'))

  6.     tmp = lines[:] + ['\n']   # 复制一份(且保证最后一行为空行)
  7.     t = tmp.count('\n')   # 空行数
  8.     for i in range(t):
  9.         empty_pos = tmp.index('\n')   
  10.         # print(empty_pos)
  11.         # group = tmp[ : empty_pos ]
  12.         # print(tmp[empty_pos-3].strip().isdigit())
  13.         # 单行字幕的情况
  14.         if tmp[empty_pos-3].strip().isdigit():   
  15.             group = tmp[:empty_pos]
  16.             # print(group)
  17.             tmp = tmp[empty_pos+1:]
  18.             print('各段字幕:\n', ''.join(group)+'\n')
  19.         else:  
  20.             # 双行字幕的情况
  21.             group = tmp[:empty_pos-2] + [''.join(tmp[empty_pos-2:empty_pos]).replace('\n', ' ')]
  22.             # print(group)
  23.             tmp = tmp[empty_pos+1:]
  24.             print('各段字幕:\n', ''.join(group)+'\n')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 08:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表