As I have mentioned in Question title, I have following INI file, which contains umpteen number of sections and each section containing umpteen lines:
....
.... (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...)
....
Now as you can see when I pasted code from external Google search pages, sometimes these codes are with extra newlines, which occur after each line, which kinda breaks the pattern and/or format of INI files which ensure there’s 2 newlines between one section’s ending and another one’s beginning and only one newline after each section’s children lines.
Now I know that with Regex we can replace multiple newlines into single newline by Find:\n\n* and Replace with: \r\n in any Text editor that supports PCRE Regex, but when there’s extra newlines spread across multiple/all children lines of a section, then how can I text process it to ensure there’s only one newline between each children lines of a section and 2 newlines between consecutive sections across the whole file ?
So after the Regex replace text processing the final output is this:
....
.... (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...)
....
Am not so well-versed in Regex so any help is appreciated…
>Solution :
In Notepad++ and Sublime Text, you can use
Find What: (\R){2,}(?!\R*\[[^][]*]$)
Replace With: $1
See the regex demo. Details:
(\R){2,}– two or more line break sequences (the last one captured is saved in Group 1 memory buffer)(?!\R*\[[^][]*]$)– a negative lookahead that fails the match if there are\R*– zero or more line break sequences\[– a[char[^][]*– zero or more chars other than[and]]– a]char$– end of a line.
In Visual Studio Code, this regex needs tweaking a bit:
(\n){2,}(?!\n*\[[^\][]*\]$)
Here, \n matches any line ending (no need for \R), and the literal square brackets needs escaping (only the [ inside a character class does not need escaping).