日期:2014-05-18  浏览次数:20511 次

请问用正则写匹配有标识的一行怎么写?谢谢!
有以下代码:
HTML code

$[s1]这里是第一个<br/>
这里显示的时候是下一行.也就是\n\r
$[s2]this is 2个
$[s3]this 是第三个
CDSN论坛
谢谢各位了
$[s4]这里第四个


现在想匹配出来:
HTML code

这里是第一个<br/>
this is 2个
this 是第三个
这里第四个


如何写正则?谢谢!

------解决方案--------------------

应该是我没理解你的意思。。

C# code

    static void Main(string[] args)
        {
            string str = @"$[s1]这里是第一个<br/>
这里显示的时候是下一行.也就是\n\r
$[s2]this is 2个
$[s3]this 是第三个
CDSN论坛
谢谢各位了
$[s4]这里第四个";
            Regex re = new Regex(@"(?is)\$\[[^]]+\](?<s1>[^\n]+)\s*.*?\$\[[^]]+\](?<s2>[^\n]+)\s*.*?\$\[[^]]+\](?<s3>[^\n]+)\s*.*?\$\[[^]]+\](?<s4>[^\n]+)\s*.*?", RegexOptions.None);
            Match ma = re.Match(str);
            Console.WriteLine(ma.Groups["s1"].Value);
            Console.WriteLine(ma.Groups["s2"].Value);
            Console.WriteLine(ma.Groups["s3"].Value);
            Console.WriteLine(ma.Groups["s4"].Value);
            Console.ReadLine();
        }

------解决方案--------------------
正则不变,替换成空就行了。
string r = Regex.Replace(s, @"(?s)\$\[.+?\][^\r\n$]+", "");
Response.Write(Server.HtmlEncode(r));