In C#, I write the following string to a string variable, carriage return and all:
在c#中,我将以下字符串写入字符串变量,回车和所有:
asdfasdfasdf
asdfas<test>asdfasdf
asdfasdf<test>asdfasdf
In Notepad2, I use this regular expression:
在Notepad2中,我使用了这个正则表达式:
<test>.*<test>
It selects this text as expected:
它按照预期选择该文本:
<test>asdfasdf
asdfasdf<test>
However, when I do this in C#:
然而,当我用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.
因此,我要寻找的是匹配任何字符的regex,无论它是控制代码还是普通字符。
2 个解决方案
#1
48
You forgot to specify that the Regex operation (specifically, the .
operator) should match all characters (not all characters except \n):
您忘记指定Regex操作(特别是,the。操作符)应该匹配所有字符(不是所有字符,除了\n):
System.Text.RegularExpressions.Regex.Replace(s, "<test>.*<test>", string.Empty, RegexOptions.Singleline);
All you needed to add was RegexOptions.Singleline
.
你只需要添加RegexOptions.Singleline。
#2
10
Use single-line mode:
使用单行模式:
Regex.Replace(s, "<test>.*<test>", "", RegexOptions.Singleline);
#1
48
You forgot to specify that the Regex operation (specifically, the .
operator) should match all characters (not all characters except \n):
您忘记指定Regex操作(特别是,the。操作符)应该匹配所有字符(不是所有字符,除了\n):
System.Text.RegularExpressions.Regex.Replace(s, "<test>.*<test>", string.Empty, RegexOptions.Singleline);
All you needed to add was RegexOptions.Singleline
.
你只需要添加RegexOptions.Singleline。
#2
10
Use single-line mode:
使用单行模式:
Regex.Replace(s, "<test>.*<test>", "", RegexOptions.Singleline);