C# 除符号外的所有字符和新行的正则表达式
我的任务是用C语言编写一个解析器,将字符串转换为html代码。现在,我正在使用正则表达式解析字符串,因此,我的代码如下所示:C# 除符号外的所有字符和新行的正则表达式,c#,regex,parsing,C#,Regex,Parsing,我的任务是用C语言编写一个解析器,将字符串转换为html代码。现在,我正在使用正则表达式解析字符串,因此,我的代码如下所示: Regex regex = new Regex(@"\n?\[s=(.*)?\](?:\n?(.*)?\n?)?\[\/s\]\n?"); MatchCollection matches = regex.Matches(CurrentPage.FAQ); foreach (Match match in matches) { <div class="slid
Regex regex = new Regex(@"\n?\[s=(.*)?\](?:\n?(.*)?\n?)?\[\/s\]\n?");
MatchCollection matches = regex.Matches(CurrentPage.FAQ);
foreach (Match match in matches)
{
<div class="slider">
<div class="n">@match.Groups[1].Value</div>
<div class="tt">@Html.Raw(match.Groups[2].Value.Replace("\r", "<br />").Replace(" ", " "))</div>
</div>
}
如果文本中有一行新行,则不进行验证。比如说
[s=hello]
This is demo list
1. Hello world!
2. List item
[/s]
我试图将我的表达式修改为\n?\[s=(.*)\](?:\n?((.\124;\ n)*)?\n?\[\/s\]\n?
,但字符串可以包含一个或多个[s]项,因此它将其作为一个项进行验证
[s=hello](BEGINING TO VALIDATE FROM HERE)This is demo text![/s]
[s=hello]
This is demo list
1. Hello world!
2. List item
(TO HERE)[/s]
这就是它将其作为一个项目进行验证的原因。使用如下的负前瞻,不要忘记启用DOTALL
(?s)
修饰符,使正则表达式中的点也匹配偶数换行符
@"(?s)\[s=([^\[\]]*)\]((?:(?!\[\/s]).)*)\[\/s]"
@"(?s)\[s=([^\[\]]*)\]((?:(?!\[\/s]).)*)\[\/s]"