我希望我能正确解释。我有下面的文本,它由 2 个部分组成,1- 正文和 2- 标题,每个部分都由“----”分隔。我可以使用 Notepad++ 正则表达式 (?s)----(.*?) 标记此数据,但就我而言,我无法弄清楚如何使用 2 组分隔行将正文和标题合并为一行。该文件有超过 9K 个部分。所以我想要合并的第一个示例集看起来像 - 鲨鱼和律师;鲨鱼和律师 - 问:为什么鲨鱼不会攻击律师?答:专业礼貌。第二组 - 猪蜈蚣;猪蜈蚣 - 当你把猪和蜈蚣杂交时,你会得到什么?培根
和腿。正如您所见,“----”之间的文本各不相同。请帮助或为我指明正确的方向。谢谢。
Sharks and Lawyers - Q: Why won't sharks attack lawyers? A: Professional courtesy.
----
Sharks and Lawyers
----
Pig-ipede - What do you get when you cross a pig and a centipede?
Bacon and legs.
----
Pig-ipede
----
Meals on Wheels - Once upon a time, there was a cat who died. When she got to heaven, God asked her how she liked being on earth. She told the Lord that it was awful -- she had to sleep in cold back alleys, where there was no food and life was hard. God told her that he was sorry it had had turned out that way -- but here, in heaven, she would be happy and He would give her the most comfortable, warm pillow to sleep on. The cat laid down upon the pillow and was happy.
A few days later, about a dozen mice that came to heaven together and God asked them how they had liked earth. The earth was no better for them than it was the cat. They explained to God that it was tough and exhausting and their feet were worn out from always running from cats and dogs and people. God felt bad for the mice and decided to give them rollerskates.
One day God sees the cat again and asked her how she was liking heaven. She explained that it was absolutely wonderful. The pillow he gave her was the most comfortable place that she had ever slept on, but even better than the pillow were the meals on wheels.
----
Meals on Wheels
----
答案1
- Ctrl+H
- 找什么:
^([\s\S]+?\R----\R)(.+)\R----\R?
- 用。。。来代替:
$2; $1
- 打钩 环绕
- 选择 正则表达式
- 取消勾选
. matches newline
- Replace all
解释:
^ # beginning of line
( # group 1, body text
[\s\S]+? # any character, 1 or more times, not greedy
\R # any kind of linebreak (i.e. \r, \n, \r\n)
---- # 4 hyphens
\R # any kind of linebreak
) # end group 1
(.+) # group 2, title
\R # any kind of linebreak
---- # 4 hyphens
\R? # any kind of linebreak, optional
替代品:
$2; $1 # content of group 2 (i.e.: the title) semicolon+space, content of group 1 (the body)
截图(之前):
截图(之后):