记事本:如何在每行的最后一行中添加包含字符串的前缀?

记事本:如何在每行的最后一行中添加包含字符串的前缀?

我正在处理聊天记录并想对其进行格式化。

它们看起来完全像这样,包括#符号:

Tuesday, February 24, 2015
##Person1 (21:22:01): hello
##Person2 (21:22:37): hi
Wednesday, February 25, 2015
##Person1 (13:12:43): hey
##Person2 (13:13:04): hey

该日期仅针对每个新一天发布,我希望它的格式类似于此,以便可以在电子表格中使用:

Tuesday, February 24, 2015
Tuesday, February 24, 2015##Person1 (21:22:01): hey
Tuesday, February 24, 2015##Person2 (21:22:37): hi
Wednesday, February 25, 2015
Wednesday, February 25, 2015##Person1 (13:12:43): hey
Wednesday, February 25, 2015##Person2 (13:13:04): hey

之后,我可以轻松删除不包含 ## 字符串的行以摆脱仅包含日期的行。

有什么方法可以让 Notepad++ 将包含日期字符串的整个最新行(如\d{1,2}, 201\d{1}$)添加到其下方每一行的开头(直到下一个实例)?

答案1

恐怕这无法在 Notepad++ 中完成。

这是完成该工作的 perl 单行程序。

perl -ane '$date = $1 if /^(\w+,\h+\w+\h+\d\d?,\h+20\d\d)/;s/^(?=##)/$date/ && print;' file.txt

如果要就地替换文件,请使用:

perl -i -ane '$date = $1 if /^(\w+,\h+\w+\h+\d\d?,\h+20\d\d)/;s/^(?=##)/$date/ && print;' file.txt

输出:

Tuesday, February 24, 2015##Person1 (21:22:01): hello
Tuesday, February 24, 2015##Person2 (21:22:37): hi
Wednesday, February 25, 2015##Person1 (13:12:43): hey
Wednesday, February 25, 2015##Person2 (13:13:04): hey

正则表达式解释:

/               # delimiter
  ^             # beginning of line
  (             # start group 1
    \w+         # 1 or more word character
    ,           # a comma
    \h+         # 1 or more horizontal spaces
    \w+         # 1 or more word character
    \h+         # 1 or more horizontal spaces
    \d\d?       # 1 or 2 digits
    ,           # a comma
    \h+         # 1 or more horizontal spaces
    20\d\d      # 20 and 2 digits
  )             # end group 1
/               # delimiter


s/              # substitute, delimiter
  ^             # beginning of line
  (?=##)        # positive lookahead, zero-length assertion that make sure we have ## at the beginning
/               # delimiter
  $date         # the date found with the preceding  regex
/               # delimiter

相关内容