简体   繁体   English

Notepad ++正则表达式将文件中的日期放在每行的开头

[英]Notepad++ regex to put date from file at start of each line

Given a file in the following format (where X is any text, without newlines):给定以下格式的文件(其中 X 是任何文本,没有换行符):

01st December 2019
0100 X
0200 X
0300 X
1745 X
02nd December 2019
0015 X
1555 X
2335 X

What would the regex be to transform it to put the date at the start of each line, and remove the lines that are just the date, eg:正则表达式将如何转换它以将日期放在每行的开头,并删除只是日期的行,例如:

01st December 2019 0100 X
01st December 2019 0200 X
01st December 2019 0300 X
01st December 2019 1745 X
02nd December 2019 0015 X
02nd December 2019 1555 X
02nd December 2019 2335 X

I know i can find the lines that start with dates by searching for [0-3][0-9][snrt[tdh] , and the start of a line by searching for ^ , but how can I say "find ^ and replace with the previous match for the date"?我知道我可以通过搜索[0-3][0-9][snrt[tdh]找到以日期开头的行,通过搜索^找到行的开头,但我怎么说“找到 ^ 和替换为日期的上一个匹配项”?

If the number of lines to be joined is not too high, you can do the following where I've limited the number of lines to 7:如果要加入的行数不是太多,您可以执行以下操作,我将行数限制为 7:

  • Ctrl + H Ctrl + H
  • Find what: ^(\d\d(?:st|nd|rd|th) \w+ \d{4})$\R(^\d{4}.+$)(?:\R(^\d{4}.+$))?(?:\R(^\d{4}.+$))?(?:\R(^\d{4}.+$))?(?:\R(^\d{4}.+$))?(?:\R(^\d{4}.+$))?查找什么: ^(\d\d(?:st|nd|rd|th) \w+ \d{4})$\R(^\d{4}.+$)(?:\R(^\d{4}.+$))?(?:\R(^\d{4}.+$))?(?:\R(^\d{4}.+$))?(?:\R(^\d{4}.+$))?(?:\R(^\d{4}.+$))?
  • Replace with: $1 $2(?3\n$1 $3)(?4\n$1 $4)(?5\n$1 $5)(?6\n$1 $6)(?7\n$1 $7)替换为: $1 $2(?3\n$1 $3)(?4\n$1 $4)(?5\n$1 $5)(?6\n$1 $6)(?7\n$1 $7)
  • CHECK Wrap around检查环绕
  • CHECK Regular expression CHECK正则表达式
  • UNCHECK . matches newline取消选中. matches newline . matches newline
  • Replace all全部替换

Explanation:解释:

^                       # beginning of line
  (                     # group 1
    \d\d                  # 2 digits (the day)
    (?:st|nd|rd|th)       # any of st or nd or rd or th
    \w+                   # 1 or more word character (the month)
    \d{4}                 # 4 digits (the year)
  )                     # end group
$                       # end of line
\R                      # any kind of linebreak
(                       # group 2
  ^                     # beginning of line
    \d{4}                 # 4 digits (the time)
    .+                    # 1 or more any character but newline
  $                     # end of line
)                       # end group 2
(?:                     # non capture group
  \R                      # any kind of linebreak
  (^\d{4} .+$)            # group 3, same pattern as in group 2
)?                      # end group, optional
(?:\R(^\d{4} .+$))?     # same as above for group 4
(?:\R(^\d{4} .+$))?     # same as above for group 5
(?:\R(^\d{4} .+$))?     # same as above for group 6
(?:\R(^\d{4} .+$))?     # same as above for group 7
                    you can add more groups if you need

Replacement:替代品:

$1 $2                   # content of group 1, space, content of group 2
(?3                     # if group 3 exists:
  \n                      # linefeed
  $1 $3                   # content of group 1, space, content of group 3
)                       # end condition
(?4\n$1 $4)             # same as above fot group 4
(?5\n$1 $5)             # same as above fot group 5
(?6\n$1 $6)             # same as above fot group 6
(?7\n$1 $7)             # same as above fot group 7

Screenshot (before):截图(之前):

在此处输入图像描述

Screenshot (after):截图(之后):

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM