有一字符串如下(每页面中这样的字符串只有一个)
string src = @"<!--start-->华夏时报<!--end-->"
为了取里面的文字内容,我用了环视
            string src = @"<!--start-->华夏时报<!--end-->"
            string sourcePattern = @"(?<=<!--jrj_final_source_start-->)[\s\S]*(?=<!--jrj_final_source_end-->)";            Regex regex = new Regex(sourcePattern, RegexOptions.IgnoreCase);
            Match match = regex.Match(src);
            if (match.Success)
            {
                Console.WriteLine(match.Value);
            }可是有些页面里面会有链接,比如
string src = @"<!--start--><a href=""http://www.chinatimes.cc/"">华夏时报</a><!--end-->";
我仍然只是想去里面的文字"华夏时报",该如何写? 请注意,<!--start--> <!--end-->两个字符串中间,如果只包括文本则只取文本,如果中间包括了链接和文本(只有一个链接),则仍然只要其文字内容,请问如何用一个正则完成。 
谢谢!

解决方案 »

  1.   

    Regex regExp = new Regex(@"<!--start-->[\s\S]*?<a[^>]+>(?<text>[\s\S]*?)</a>[\s\S]*<!--end-->");
    string src = @"<!--start--><a href=""http://www.chinatimes.cc/"">华夏时报</a><!--end-->";
    Match match = regex.Match(src);if (match.Success)
    {
        Console.WriteLine(match.Groups["text"].Value);
    }
      

  2.   

    Regex regExp = new Regex(@"<!--start-->[\s\S]*?<a[^>]+>(?<text>[\s\S]*?)</a>[\s\S]*<!--end-->");
    string src = @"<!--start--><a href=""http://www.chinatimes.cc/"">华夏时报</a><!--end-->";
    Match match = regex.Match(src);if (match.Success)
    {
        Console.WriteLine(match.Groups["text"].Value);
    }
      

  3.   

    本帖最后由 lxcnn 于 2010-05-31 13:04:59 编辑
      

  4.   

    由于我有些项目上的需求,必须一个正则搞定,最后这样写的,请两位指教
    string sourcePattern = @"
    <!--start-->
    (
    [\s\S]*<a[^>]+>(?<text>[\s\S]*?)</a>[\s\S]*
    |
    (?<text>[\s\S]*?)
    )
    <!--end-->";
      

  5.   

    怎么拆开了?拆开要写(?x)string sourcePattern = @"
    <!--start-->
    (
    [\s\S]*<a[^>]+>(?<text>[\s\S]*?)</a>[\s\S]*
    |
    (?<text>[\s\S]*?)
    )
    <!--end-->";
      

  6.   

    Regex regExp = new Regex(@"<!--start-->[\s\S]*?<a[^>]+>(?<text>[\s\S]*?)</a>[\s\S]*<!--end-->");
    string src = @"<!--start--><a href=""http://www.chinatimes.cc/"">华夏时报</a><!--end-->";
    Match match = regex.Match(src);if (match.Success)
    {
        Console.WriteLine(match.Groups["text"].Value);
    }