C# Regex.replace不会将多行文本行替换为html块的起始字符
我对多行文本中c#explicit中的正则表达式替换函数有问题。在本文中,我有不同的行,有时在开始时带有特殊字符(例如:&和&&&),行必须转换为html。它类似于降价转换 下面是一个示例文本:C# Regex.replace不会将多行文本行替换为html块的起始字符,c#,regex,replace,C#,Regex,Replace,我对多行文本中c#explicit中的正则表达式替换函数有问题。在本文中,我有不同的行,有时在开始时带有特殊字符(例如:&和&&&),行必须转换为html。它类似于降价转换 下面是一个示例文本: This is a normal demo text. &Here an other demo text. And one more demo text. &&Here will continue this text. Bla bla blaaa... 文本应在以下文本替换为开
This is a normal demo text.
&Here an other demo text.
And one more demo text.
&&Here will continue this text.
Bla bla blaaa...
文本应在以下文本替换为开始和结束html标记后显示:
This is a normal demo text.
<b>Here an other demo text.</b>
And one more demo text.
<em>Here will continue this text...</em>
Bla bla blaaa...
这是一个普通的演示文本。
这里是另一个演示文本。
还有一个演示文本。
这里将继续这段文字。。。
呜呜呜呜。。。
为了替换和(&I),我创建了以下c#代码:
private string StartConvert(str text)
{
text=Regex.Replace(text,“^[&]{1}((?![&]).+)$”,“$1”,RegexOptions.Multiline);
text=Regex.Replace(text,“^[&]{2}((?![&]).+)$”,“$1”,RegexOptions.Multiline);
}
运行后,我将得到以下错误信息:
This is a normal demo text.
</b>ere an other demo text.
And one more demo text.
</em>ere will continue this text...
Bla bla blaaa...
这是一个普通的演示文本。
还有其他演示文本。
还有一个演示文本。
我将继续这段文字。。。
呜呜呜呜。。。
为什么这不能正常工作,为什么我只得到行前面的闭合标记。如果是单行,则可以正常工作,但不能在多行中工作
感谢您的帮助这不是解决方案,但我可以告诉您问题不在正则表达式中,问题在于打印文本。我看到结果字符串完全正常 所以我做了这样的检查
string[] strings = text.Split(' ');
foreach (var s in xa)
{
Console.WriteLine(s);
}
您可以在调试时检查字符串,是否正常?我假定您的文件是Windows EOF编码的:
\r\n
表达式:“^[&]{1}((?![&]).+)$”
将与另一个演示文本相匹配。\r\n
后面的参考((?![&]).+)
将是另一个演示文本。\r
使用此替换字符串:这里是另一个演示文本。\r
用
如何分配输入来重写H
?我也测试过这一点,Regex在Regex maker中工作得很好,但当涉及到C时,它跳过了HY。我们的代码对我来说很有效
string[] strings = text.Split(' ');
foreach (var s in xa)
{
Console.WriteLine(s);
}
<b>Here an other demo text.\r
^___________________________|
</b>ere an other demo text.\r
|__^