有一字符串如下(每页面中这样的字符串只有一个)
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-->两个字符串中间,如果只包括文本则只取文本,如果中间包括了链接和文本(只有一个链接),则仍然只要其文字内容,请问如何用一个正则完成。
谢谢!
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-->两个字符串中间,如果只包括文本则只取文本,如果中间包括了链接和文本(只有一个链接),则仍然只要其文字内容,请问如何用一个正则完成。
谢谢!
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);
}
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);
}
string sourcePattern = @"
<!--start-->
(
[\s\S]*<a[^>]+>(?<text>[\s\S]*?)</a>[\s\S]*
|
(?<text>[\s\S]*?)
)
<!--end-->";
<!--start-->
(
[\s\S]*<a[^>]+>(?<text>[\s\S]*?)</a>[\s\S]*
|
(?<text>[\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);
}