有以下代码:$[s1]这里是第一个<br/>
这里显示的时候是下一行.也就是\n\r
$[s2]this is 2个
$[s3]this 是第三个
CDSN论坛
谢谢各位了
$[s4]这里第四个现在想匹配出来:这里是第一个<br/>
this is 2个
this 是第三个
这里第四个如何写正则?谢谢!

解决方案 »

  1.   

    string s = @"$[s1]这里是第一个<br/>
    这里显示的时候是下一行.也就是\n\r
    $[s2]this is 2个
    $[s3]this 是第三个
    CDSN论坛
    谢谢各位了
    $[s4]这里第四个";
    MatchCollection matches = Regex.Matches(s, @"(?m)^\$\[.+?\].*?$");
    foreach (Match match in matches)
    Response.Write(Server.HtmlEncode(match.Value) + "<br/>");
      

  2.   


    改一下括号打反了。。汗(?<=\])[^\n]+
      

  3.   

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

  4.   

    谢谢各位.但还有一个问题.如果我想分组呢?
    也就是s1,s2,s3....做为一组,这里是第一个<br/>
    this is 2个
    this 是第三个
    这里第四个
    做为一组.
      

  5.   


    做为一组是什么意思,foreach循环中第1次的匹配到的是:这里是第一个<br/>
    第2次的匹配到的是:this is 2个
    第3次的匹配到的是:this 是第三个
    第4次的匹配到的是:这里第四个你的意思是什么?
      

  6.   

    谢谢,此正则匹配是可以了.想分组呢?
    也就是s1,s2,s3....做为一组,匹配的又做为一组.如何写了?
      

  7.   

    就是想用C#里的:
    match.Groups["S"].Value;
    match.Groups["LineText"].Value;
    这样的.
    应该写成正则类似这种(?<LineText>[^\{\}:]+)上面是以LineText为组名.这样子.
      

  8.   


    应该是我没理解你的意思。。
        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();
            }
      

  9.   

    我是用来提取文本中的字符的.需要用于s1,s2....这里的标识.
    刚刚根据1楼我写了如下正则(?m)^\$\[(?<Name>.+?)\](?<LineText>.*?$)但是弄了下,还是不能满足要求.原因是
    如果代码是这样:
    $[s1]这里是第一个<br/>
    这里显示的时候是下一行.也就是\n\r
    $[s2]this is 2个$[s3]this 是第三个
    CDSN论坛
    谢谢各位了
    $[s4]这里第四个这里
    $[s2]this is 2个$[s3]this 是第三个
    就匹配不出来了.
    之前是想我提取的会换行 ,但是想想,也有可能不换行.就像上面的代码一样.如何写?非常感谢 !
      

  10.   

    string s = @"$[s1]这里是第一个<br/>
    这里显示的时候是下一行.也就是\n\r
    $[s2]this is 2个$[s3]this 是第三个
    CDSN论坛
    谢谢各位了
    $[s4]这里第四个";
    MatchCollection matches = Regex.Matches(s, @"(?s)\$\[.+?\][^\r\n$]+");
    foreach (Match match in matches)
    Response.Write(Server.HtmlEncode(match.Value) + "<br/>");
      

  11.   

    谢谢dalmeeme.
    用(?s)\$\[(?<Name>.+?)\](?<LineText>[^\r\n$]+)成功匹配.
    到这里本来应该结束了.
    但是为多了解一下,我还想多问一个问题.
    就是如果是"反匹配"呢?
    比如,现在用这个(?s)\$\[(?<Name>.+?)\](?<LineText>[^\r\n$]+)匹配出来了$[s1]这里是第一个<br/>
    $[s2]this is 2个$[3]this 是第三个
    $[s4]这里第四个然后我想取没有匹配的,也就是剩下的.这里显示的时候是下一行.也就是\n\r
    CDSN论坛
    谢谢各位了如何做?谢谢!
      

  12.   

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