kelly divine
kelly divine

Reputation: 19

Replace MultiLines with Multilines

I have 2 richboxes.
richbox1 contains full text:

line1

line2

line3

line4
.
.
.

richbox2 contains part of the text:

line2

line3

I want to replace lines in richbox2 with EmptyString in richbox1. But this regular expression not working for me.

richbox1.Text = Regex.Replace(richbox1.Text, richbox2.Text, string.Empty);

Upvotes: 0

Views: 134

Answers (2)

buckley
buckley

Reputation: 14129

The question phrased in its current form does not require a regex. Just use a regular replace.

Your solution takes the string from richbox1 and interprets it as a regex. Characters that have special meaning in a regex will be interpreted in a way you don't want.

Update 1

The regular replace respects newlines. This code confirms

var s1 = @"line1
line2";

var s2 = @"line1
line2
line3
line4";

s2.Replace(s1,"").Dump();

Update 2

Replace

^\s+|\s+$

With nothing. This will include linebreaks which are counted as white space.

Upvotes: 1

Mitchell
Mitchell

Reputation: 7087

This example code should work:

foreach(string line in richbox2.Lines) 
{
    richbox1.Text = richbox2.Text.Replace(line, string.Empty); 
}

Upvotes: 1

Related Questions