a1204773
a1204773

Reputation: 7043

Replace new line regex with a string

This is a part of my text file:

[Script Info]
Title: Default Aegisub file
ScriptType: v4.00+
WrapStyle: 0
ScaledBorderAndShadow: yes
Collisions: Normal
Scroll Position: 0
Active Line: 0
Video Zoom Percent: 1
YCbCr Matrix: None

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR,MarginV, Encoding
Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1

[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:0:03.84,0:0:07.17,Default,,0,0,0,,An Aztec temple.
Dialogue: 0,0:0:08.88,0:0:10.97,Default,,0,0,0,,Aztecs this far south?
Dialogue: 0,0:0:11.1,0:0:13.84,Default,,0,0,0,,Dr. Covas, we must stop.
This is sacred ground.
Dialogue: 0,0:0:13.88,0:0:15.85,Default,,0,0,0,,This could be the biggest
archeological discovery
Dialogue: 0,0:0:15.88,0:0:16.87,Default,,0,0,0,,of the 21st century.

I need to make it like that (I skipped some part to show you the changed result)

Dialogue: 0,0:0:03.84,0:0:07.17,Default,,0,0,0,,An Aztec temple.
Dialogue: 0,0:0:08.88,0:0:10.97,Default,,0,0,0,,Aztecs this far south?
Dialogue: 0,0:0:11.1,0:0:13.84,Default,,0,0,0,,Dr. Covas, we must stop.\NThis is sacred ground.
Dialogue: 0,0:0:13.88,0:0:15.85,Default,,0,0,0,,This could be the biggest\Narcheological discovery
Dialogue: 0,0:0:15.88,0:0:16.87,Default,,0,0,0,,of the 21st century.

I tried a lot of things like: LoadedFile = Regex.Replace(LoadedFile, @"(?<=Dialogue.+)[\n\r]+^(Dialogue)", "\\N");

In words I want to replace the new line with \N only if text after new line is not "Dialogue"

Upvotes: 1

Views: 220

Answers (1)

VladL
VladL

Reputation: 13033

string LoadedFile = File.ReadAllText("1.txt");
string result = Regex.Replace(LoadedFile, "(Dialogue.+?)\r\n^((?!Dialogue).*)$", "$1\\N$2", RegexOptions.Multiline);

Upvotes: 1

Related Questions