正则表达式小白问题:我想获取类似:<title>网页标题</title>中“网页标题”这个内容,应该如何编写一个正则表达式来匹配呢?谢谢!

解决方案 »

  1.   

    <title\s*>(.*?)</title>
    $1(?<=<title\s*>).*?(?=</title>)
      

  2.   

    第一个表达式获取出来的内容还是包含 <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");
    }
      

  3.   

    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);
      

  4.   

    多谢楼上的老兄!
    楼上的这个可以了,但如果<title>中间有空格的话还是不能正常获取。
      

  5.   

    别光呀,如果有title中间空格还是不能正常获取
      

  6.   

    自己尝试了一下将fangxinggood(JustACoder)提供的表达式修改为:
    <title\s*>(?<content>\w.*)</title>
    即可处理字符串中间包含空格的问题。多谢楼上各位,结贴给分!
      

  7.   

    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);