Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 除符号外的所有字符和新行的正则表达式_C#_Regex_Parsing - Fatal编程技术网

C# 除符号外的所有字符和新行的正则表达式

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

我的任务是用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="slider">
        <div class="n">@match.Groups[1].Value</div>
        <div class="tt">@Html.Raw(match.Groups[2].Value.Replace("\r", "<br />").Replace(" ", "&nbsp;"))</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]"