正如我在问题标题中提到的,我有以下INI文件,其中包含无数节,每一节包含无数行:
....
.... (many more section up above)
....
[Zynga Games *]
Section=Games
DetectFile=%LocalAppData%\Zynga
FileKey1=%LocalAppData%\Zynga\Logs|*.*|RECURSE
[*Microsoft Windows Game Statistics]
LangSecRef=3025
DetectOS=6.0
DetectFile=%localappdata%\Microsoft Games\
Default=False
FileKey1=%localappdata%\Microsoft Games\Chess Titans\|chesstitans.xml
FileKey2=%localappdata%\Microsoft Games\Freecell\|freecell.xml
FileKey3=%localappdata%\Microsoft Games\Hearts\|hearts.xml
FileKey4=%localappdata%\Microsoft Games\Mahjong Titans\|mahjong titans.xml
FileKey8=%localappdata%\Microsoft Games\Minesweeper\|minesweeper.xml
FileKey5=%localappdata%\Microsoft Games\Purble Place\|purble place.xml
FileKey6=%localappdata%\Microsoft Games\Solitaire\|solitaire.xml
FileKey7=%localappdata%\Microsoft Games\Spider Solitaire\|spider solitaire.xml
[iMVU Cache]
LangSecRef=3022
DectectFile=%appdata%\IMVUClient\IMVUClient.exe
Default=False
FileKey1=%appdata%\IMVU\cache\|*.*|REMOVESELF|
FileKey2=%appdata%\IMVU\AssetCache\|*.*|REMOVESELF|
FileKey3=%appdata%\IMVU\PixmapCache\|*.*|REMOVESELF|
....
.... (many more could be below too...)
....现在您可以看到,当我从外部Google搜索页面粘贴代码时,有时这些代码带有额外的换行符,这些代码出现在每一行之后,这在一定程度上打破了INI文件的模式和/或格式,这确保了在一个部分的结束和另一个开始之间有两条新行,在每个部分的子行之后只有一条换行符。
现在我知道,使用Regex,我们可以通过Find:__\n\n*将多个换行符替换为单个换行符,并在任何支持PCRE的文本编辑器中替换为:\r\n,但是当有额外的换行符分布在节的多/所有子行时,我如何处理它以确保节的每个子行之间只有一条换行符,整个文件中的连续段之间只有2条换行线?
因此,在Regex替换文本处理之后,最后的输出如下:
....
.... (many more section up above)
....
[Zynga Games *]
Section=Games
DetectFile=%LocalAppData%\Zynga
FileKey1=%LocalAppData%\Zynga\Logs|*.*|RECURSE
[*Microsoft Windows Game Statistics]
LangSecRef=3025
DetectOS=6.0
DetectFile=%localappdata%\Microsoft Games\
Default=False
FileKey1=%localappdata%\Microsoft Games\Chess Titans\|chesstitans.xml
FileKey2=%localappdata%\Microsoft Games\Freecell\|freecell.xml
FileKey3=%localappdata%\Microsoft Games\Hearts\|hearts.xml
FileKey4=%localappdata%\Microsoft Games\Mahjong Titans\|mahjong titans.xml
FileKey8=%localappdata%\Microsoft Games\Minesweeper\|minesweeper.xml
FileKey5=%localappdata%\Microsoft Games\Purble Place\|purble place.xml
FileKey6=%localappdata%\Microsoft Games\Solitaire\|solitaire.xml
FileKey7=%localappdata%\Microsoft Games\Spider Solitaire\|spider solitaire.xml
[iMVU Cache]
LangSecRef=3022
DectectFile=%appdata%\IMVUClient\IMVUClient.exe
Default=False
FileKey1=%appdata%\IMVU\cache\|*.*|REMOVESELF|
FileKey2=%appdata%\IMVU\AssetCache\|*.*|REMOVESELF|
FileKey3=%appdata%\IMVU\PixmapCache\|*.*|REMOVESELF|
....
.... (many more could be below too...)
....我对Regex不太熟悉,所以我们很感谢你的帮助.
发布于 2022-01-30 16:29:32
在Notepad++和崇高文本中,您可以使用
找到什么:(\R){2,}(?!\R*\[[^][]*]$)
替换为:$1
见regex演示。详细信息
(\R){2,} -两个或多个中断行序列(最后捕获的一个保存在第1组内存缓冲区中)(?!\R*\[[^][]*]$) -如果有,就会导致匹配失败。\R* -零或多个断线序列\[ -a [ char[^][]* - [和]以外的零或多个字符] -a ] char$ -线的末端。在Visual代码中,此正则表达式需要稍加调整:
(\n){2,}(?!\n*\[[^\][]*\]$)在这里,\n匹配任何行的结尾(不需要\R),而文字方括号需要转义(只有字符类中的[不需要转义)。
发布于 2022-01-31 09:57:37
作弊和抄袭Wiktor的答案,
这段代码给了我编辑的结果.
查找:(?!\n*\[)(\n){2,}
替换为:$1
https://stackoverflow.com/questions/70916565
复制相似问题