zoukankan      html  css  js  c++  java
  • C#: 获取网页中匹配代码的正则 (获得字符串中开始和结束字符串中间得值)

    如:
    <div>1div</div>
    <a>1a</a>
    <p>1p</p>
    <p>2p</p>
    <div>2div</div>
    <a>2a</a>
    <p>3p</p>
    <p>4p</p>
    <a>3a</a>
    <p>5p</p>
    <div>3div</div>
    <a>4a</a>
    <p>6p</p>
    <span>1span</span>

    现在的问题是:有N多DIV,N多p,N多A标签以及最多1个span,想只获取所有p里的内容以及最后一个span里的内容(其中获取P的内容有一个条件,那就是只有前面有一个A标签的P的内容才会被获取),span或许有或许没有,如果有就获取,如果没有就不获取求:
    C#的正则表达式
    using System.Text.RegularExpressions;
    代码
     string restult = "";
                
    foreach(Match m in Regex.Matches(str ,@"(?ins)(?<=(</a>\s*<(?<mark>p[^>]*>)|<(?<mark>span)[^>]*>))[\s\S]+?(?=</\k<mark>)"))
                {
                    restult 
    +=m.Value;//就是你要的结果

                    MessageBox.Show(m.Value);
                }

    或是用
    foreach(Match m in Regex.Matches(yourHtml,@"(?is)(</a>\s*<(?<mark>p[^>]*>)|<(?<mark>span)[^>]*>)(?<data>[\s\S]+?)</\k<mark>"))
    {
        m.Groups[
    "data"].Value;//
    }

    或是>>>>>>获得字符串中开始和结束字符串中间得值

    代码
      #region 获得字符串中开始和结束字符串中间得值
            
    /// <summary>
            
    /// 获得字符串中开始和结束字符串中间得值
            
    /// </summary>
            
    /// <param name="begin">开始匹配标记</param>
            
    /// <param name="end">结束匹配标记</param>
            
    /// <param name="html">Html字符串</param>
            
    /// <returns>返回中间字符串</returns>
            public static MatchCollection GetMidValue(string begin, string end, string html)
            {
                Regex reg 
    = new Regex("(?<=(" + begin + "))[.\\s\\S]*?(?=(" + end + "))", RegexOptions.Multiline | RegexOptions.Singleline);
                
    return reg.Matches(html);
            }
            
    #endregion

    代码
     /// <summary> 
            
    /// 获得字符串中开始和结束字符串中间得值 
            
    /// </summary> 
            
    /// <param name="str"></param> 
            
    /// <param name="s">开始</param> 
            
    /// <param name="e">结束</param> 
            
    /// <returns></returns> 
            private string getvalue(string str, string start, string end) 
            {
                Regex rg 
    = new Regex("(?<=(" + start + "))[.\\s\\S]*?(?=(" + end + "))", RegexOptions.Multiline | RegexOptions.Singleline); 
         
                
    return rg.Match(str).Value;            
            }

    //正则抽取单个Table , 可根据table内的某个标识字符, good !

     如果仅仅是以“会员资料”这样的做为参考标识,用我上面写的稍稍改造就可以了,问题的复杂在于,如果以“00”或者“444”做为参考标识,就要考虑到<table>标签嵌套的问题,既要保证取包含参考标识的最内层<table>,又要保证<table>和</table>配对匹配

    代码
     Match mm = Regex.Match(html, @"<table[^>]*>(((<table[^>]*>(?<o>)|</table>(?<-o>)|(?!</?table)[\s\S])*)(?(o)(?!)))\b" + "会员资料" + @"\b(?:(?!<table[^>]*>)[\s\S])*?(((<table[^>]*>(?<o>)|</table>(?<-o>)|(?!</?table)[\s\S])*)(?(o)(?!)))</table>", RegexOptions.IgnoreCase);

    输入的参考标识中如果有正则中有特殊意义的字符,需要对其进行预处理,另外需要在程序中进行异常处理,这个自己处理下吧
    如果源字符串中同时多处出现输入的参考标识,这里取第一个出现的参考标识所在的<table>


     

    //正则抽取单个Table中 , 解析tb中的内容.........

    代码
     Match mm = Regex.Match(html, @"<table[^>]*>(((<table[^>]*>(?<o>)|</table>(?<-o>)|(?!</?table)[\s\S])*)(?(o)(?!)))\b" + "会员输赢资料" + @"\b(?:(?!<table[^>]*>)[\s\S])*?(((<table[^>]*>(?<o>)|</table>(?<-o>)|(?!</?table)[\s\S])*)(?(o)(?!)))</table>", RegexOptions.IgnoreCase);
                
    if (mm.Success)
                {
                    
    //MessageBox.Show(mm.Value);

                    
    //MatchCollection mdd = GetMidValue("<td", "</td>", mm.Value);
                    
    //foreach (Match m in mdd)
                    
    //{
                    
    //    for (int i = 1; i < m.Groups.Count; i++)
                    
    //    {                       
                    
    //        restult += m.Groups[i].Value;//就是你要的结果
                    
    //    }                    
                    
    //}

                    MatchCollection mc 
    = Regex.Matches(mm.Value, @"<td[^>]*>\s*(?<content>[\s\S]*?)\s*</td>", RegexOptions.IgnoreCase);
                    
    foreach(Match m in mc)
                    {
                        
    for (int i = 1; i < m.Groups.Count; i++)
                        {
                            restult 
    += m.Groups[i].Value + "\n";
                        }
                    }
                    MessageBox.Show(restult);
                }

     

  • 相关阅读:
    不常用的cmd命令
    js获取宽度
    Marshaling Data with Platform Invoke 概览
    Calling a DLL Function 之三 How to: Implement Callback Functions
    Marshaling Data with Platform Invoke 之四 Marshaling Arrays of Types
    Marshaling Data with Platform Invoke 之一 Platform Invoke Data Types
    Marshaling Data with Platform Invoke 之三 Marshaling Classes, Structures, and Unions(用时查阅)
    Calling a DLL Function 之二 Callback Functions
    WCF 引论
    Marshaling Data with Platform Invoke 之二 Marshaling Strings (用时查阅)
  • 原文地址:https://www.cnblogs.com/Fooo/p/1858121.html
Copyright © 2011-2022 走看看