使用正则表达式将原始输入修改为看起来像特定格式的公交车时间表?

Modify raw input to look like a bus timetable in specific format using regex?

我已经尝试解决这个问题很长时间了,但似乎无法找到可以立即或以我喜欢的方式工作的解决方案。

我的输入如下所示:

0430
0500 25 50
0615 34 51
0708 26 43

我需要把它变成这样:

04:30
05:00,05:25,05:50
06:15,06:34,06:51
07:08,07:26,07:43

由于这只是输入的一部分并且手动替换所有内容不是一个选项,我的猜测是最好的选择是使用正则表达式。

需要做的事情:

  1. 在前两个密码后插入冒号(类似于 (^\d{2}) 然后用 : 做 replace/substitution)
  2. 将每个 space 替换为逗号 + 前两个密码 + 冒号。 我的想法是捕获组 (^\d{2}:),然后用 , 替换所有 space(每行),但我似乎无法找到实现它的方法。

我使用 regex101.com 来做这件事,所以如果您对如何做或在哪里做有任何建议(或者即使正则表达式不是做这件事的方法,还有什么其他方法你会推荐)任何帮助将不胜感激。

提前致谢!

这是一种使用 Notepad++ 完成这项工作的方法:

  • Ctrl+H
  • 查找内容:^(\d\d)(\d\d)(?:\h+(\d\d)\h+(\d\d))?
  • 替换为:(?3,\:,\::)
  • 检查 环绕
  • 检查 正则表达式
  • 全部替换

解释:

^               # beginning of line
  (\d\d)        # group 1, 2 digits
  (\d\d)        # group 2, 2 digits
  (?:           # non capture group
    \h+         # 1 or more horizontal spaces
    (\d\d)      # group 3, 2 digits
    \h+         # 1 or more horizontal spaces
    (\d\d)      # group 4, 2 digits
  )?            # end group, optional

替换:

              # content of group 1
              # content of group 2
(?3             # if group 3 exists
  ,\:           # a comma then content of group 1 and 3
  ,\:           # a comma then content of group 1 and 4
  :             # else nothing
)               # end conditional

屏幕截图(之前):

屏幕截图(之后):