landi
landi

Reputation: 343

Repeating C# Regex replace

I want to create a small tool to convert some code.

Most of it can be done by Regex, so I do not need an actual parser.

But there is a problem with repeating patterns. Below a simplified sample:

var nl = Environment.NewLine;
var temp = 
@"Other things
End
End
Other things";

temp = Regex.Replace(temp, @"(\r\nEnd\r\n)+",@"" + nl + "}" + nl + "");

This will return

Other things
}
End
Other things

So the second End is not replaced. (except if I execute the same Regex.Replace twice)

Any idea how to solve this?

Best regards

Upvotes: 0

Views: 244

Answers (1)

xanatos
xanatos

Reputation: 111850

It happens because matches can't overlap. You have

Other things\r\n
End\r\n
End\r\n
Other things\r\n

that is matched as

Other things***From Here***\r\n
End\r\n***To Here***
End\r\n
Other things

The regex can't match the second \r\nEnd\r\n because the prefixed \r\n has already been captured.

Try:

temp = Regex.Replace(temp, @"(?<=\r\n)End(?=\r\n)+", "}");

Using the non-capturing grouping (?<=) and (?=) you can solve the problem (but note that being non-capturing, you can't replace what is matched in the (?<=) and (?=), so the difference in the replaced text "}")

Upvotes: 4

Related Questions