0

I would like to remove the lines of a richtextbox that contains a number between two underscores.

On the richtextbox, I have the following values (examples):

Z_OBJECTS
Z_1_OBJECTS
Z_DEBUG
Z_1_DEBUG
Z_2_DEBUG
Z_TI_PROJECTS
Z_1_TI_PROJECTS

I want to keep only the lines Z_OBJECT, Z_TI_PROJECTS and Z_DEBUG, removing the lines that contains 1_, etc.

I'm using this function here, which works fine, but I think Regex would be better:

int total = 22;
for (int i = 1; i <= total; i++)
{
    List<string> finalLines = richTextBox1.Lines.ToList();
    finalLines.RemoveAll(x => x.StartsWith("Z_" + i + "_"));
    richTextBox1.Lines = finalLines.ToArray();
}

What I got in Regex is this:

richTextBox1.Text = Regex.Replace(richTextBox1.Text, @"^Z_\d*_", "", RegexOptions.Multiline);

Which will not remove the rest of the line.

Appreciate any help.

4

1 回答 1

2

只需将匹配扩展到该行的其余部分:

@"^Z_\d+_.*$"

而且我认为最好使用\d+(或[0-9]+),因为您要明确查找下划线之间的数字。

于 2013-10-08T21:04:00.363 回答