Notepad++ 正则表达式将文件中的日期放在每行的开头
Notepad++ regex to put date from file at start of each line
给定以下格式的文件(其中 X 是任何文本,没有换行符):
01st December 2019
0100 X
0200 X
0300 X
1745 X
02nd December 2019
0015 X
1555 X
2335 X
将正则表达式转换为将日期放在每行的开头,并删除仅包含日期的行,例如:
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
我知道我可以通过搜索 [0-3][0-9][snrt[tdh]
找到以日期开头的行,通过搜索 ^
可以找到以日期开头的行,但我怎么能说 "find ^ and replace with the previous match for the date" ?
如果加入的行数不是太多,你可以按照下面的方法,我把行数限制在7行:
- Ctrl+H
- 查找内容:
^(\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} .+$))?
- 替换为:
(?3\n )(?4\n )(?5\n )(?6\n )(?7\n )
- 检查 环绕
- 检查 正则表达式
- 取消选中
. matches newline
- 全部替换
解释:
^ # 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
替换:
# content of group 1, space, content of group 2
(?3 # if group 3 exists:
\n # linefeed
# content of group 1, space, content of group 3
) # end condition
(?4\n ) # same as above fot group 4
(?5\n ) # same as above fot group 5
(?6\n ) # same as above fot group 6
(?7\n ) # same as above fot group 7
截图(之前):
截图(后):
给定以下格式的文件(其中 X 是任何文本,没有换行符):
01st December 2019
0100 X
0200 X
0300 X
1745 X
02nd December 2019
0015 X
1555 X
2335 X
将正则表达式转换为将日期放在每行的开头,并删除仅包含日期的行,例如:
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
我知道我可以通过搜索 [0-3][0-9][snrt[tdh]
找到以日期开头的行,通过搜索 ^
可以找到以日期开头的行,但我怎么能说 "find ^ and replace with the previous match for the date" ?
如果加入的行数不是太多,你可以按照下面的方法,我把行数限制在7行:
- Ctrl+H
- 查找内容:
^(\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} .+$))?
- 替换为:
(?3\n )(?4\n )(?5\n )(?6\n )(?7\n )
- 检查 环绕
- 检查 正则表达式
- 取消选中
. matches newline
- 全部替换
解释:
^ # 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
替换:
# content of group 1, space, content of group 2
(?3 # if group 3 exists:
\n # linefeed
# content of group 1, space, content of group 3
) # end condition
(?4\n ) # same as above fot group 4
(?5\n ) # same as above fot group 5
(?6\n ) # same as above fot group 6
(?7\n ) # same as above fot group 7
截图(之前):
截图(后):