Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# regular expression to match ANY character?

Tags:

c#

regex

In C#, I write the following string to a string variable, carriage return and all:

asdfasdfasdf asdfas<test>asdfasdf  asdfasdf<test>asdfasdf 

In Notepad2, I use this regular expression:

<test>.*<test> 

It selects this text as expected:

<test>asdfasdf  asdfasdf<test> 

However, when I do this in C#:

System.Text.RegularExpressions.Regex.Replace(s, "<test>.*<test>", string.Empty); 

It doesn't remove the string. However, when I run this code on a string without any carriage returns, it does work.

So what I am looking for is a regex that will match ANY character, regardless whether or not it is a control code or a regular character.

like image 811
oscilatingcretin Avatar asked Apr 27 '11 19:04

oscilatingcretin


1 Answers

You forgot to specify that the Regex operation (specifically, the . operator) should match all characters (not all characters except \n):

System.Text.RegularExpressions.Regex.Replace(s, "<test>.*<test>", string.Empty, RegexOptions.Singleline); 

All you needed to add was RegexOptions.Singleline.

like image 189
qJake Avatar answered Sep 23 '22 02:09

qJake