第一个表达式获取出来的内容还是包含 <title> </title>的,我的目的是想获取title标签内容,不包含<title> </title>的。测试代码为: string stringRegex = @"<title\s*>(.*?)</title>"; Regex regex = new Regex(stringRegex);MatchCollection matches = regex.Matches(htmlCode); // htmlCode是输入的html页面内容System.Collections.IEnumerator enumerator = matches.GetEnumerator(); while (enumerator.MoveNext() && enumerator.Current != null) { Match match = (Match)(enumerator.Current); Console.WriteLine(match.Value + "\r\n"); }
Regex regex = new Regex(@"<title\s*>(?<content>\w*)</title>"); string strHTML = "<title>abc</title>"; Match match = regex.Match(strHTML); MessageBox.Show(match.Groups["content"].Value);
Regex regex = new Regex(@"<title\s*.*>(?<content>.*)</title>"); string strHTML = "<title a='b'>abc</title>"; Match match = regex.Match(strHTML); MessageBox.Show(match.Groups["content"].Value);
$1(?<=<title\s*>).*?(?=</title>)
string stringRegex = @"<title\s*>(.*?)</title>";
Regex regex = new Regex(stringRegex);MatchCollection matches = regex.Matches(htmlCode); // htmlCode是输入的html页面内容System.Collections.IEnumerator enumerator = matches.GetEnumerator();
while (enumerator.MoveNext() && enumerator.Current != null)
{
Match match = (Match)(enumerator.Current);
Console.WriteLine(match.Value + "\r\n");
}
string strHTML = "<title>abc</title>";
Match match = regex.Match(strHTML);
MessageBox.Show(match.Groups["content"].Value);
楼上的这个可以了,但如果<title>中间有空格的话还是不能正常获取。
<title\s*>(?<content>\w.*)</title>
即可处理字符串中间包含空格的问题。多谢楼上各位,结贴给分!
string strHTML = "<title a='b'>abc</title>";
Match match = regex.Match(strHTML);
MessageBox.Show(match.Groups["content"].Value);