zoukankan      html  css  js  c++  java
  • C#正则_取出标签内的内容(非贪婪)

    using System.Text.RegularExpressions;

     /// <summary>
            /// 执行正则提取出值
            /// </summary>
            /// <param name="RegexString">正则表达式</param>
            /// <param name="HtmlCode">HtmlCode源代码</param>
            /// <returns>数组</returns>
            public static string[] GetRegValueAarry(string RegexString, string HtmlCode)
            {
                string[] MatchVale = new String[30];//初始化数组
                int i = 0;
                Regex r = new Regex(RegexString);

                for (Match m = r.Match(HtmlCode); m.Success; m = m.NextMatch())
                {
                    MatchVale[i] = m.Value.ToString();
                    i++;
                }

                return MatchVale;
            }

            /// <summary>
            /// 获取标签内的内容(只取出第一个)
            /// </summary>
            /// <param name="code"></param>
            /// <param name="wordsBegin"></param>
            /// <param name="wordsEnd"></param>
            /// <returns></returns>
            public static string SniffwebCode(string code, string wordsBegin, string wordsEnd)
            {
                string NewsTitle = "";
                Regex regex1 = new Regex("" + wordsBegin + @"(?<content>[sS]+?)" + wordsEnd + "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                for (Match match1 = regex1.Match(code); match1.Success; match1 = match1.NextMatch())
                {
                    NewsTitle += match1.Groups["content"].ToString();
                    break;
                }
                return NewsTitle;
            }

            // <summary>
            /// 获取标签内的内容并用~连上
            /// </summary>
            /// <param name="code"></param>
            /// <param name="wordsBegin"></param>
            /// <param name="wordsEnd"></param>
            /// <returns></returns>
            public static string SniffwebCodeSplit(string code, string wordsBegin, string wordsEnd)
            {
                string NewsTitle = "";
                Regex regex1 = new Regex("" + wordsBegin + @"(?<content>[sS]+?)" + wordsEnd + "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                for (Match match1 = regex1.Match(code); match1.Success; match1 = match1.NextMatch())
                {
                    NewsTitle += match1.Groups["content"].ToString() + "~";
                }
                return NewsTitle;
            }

  • 相关阅读:
    自定义动画(仿Win10加载动画)
    jquery 仿windows10菜单效果下载
    mac os+selenium2+Firefox驱动+python3
    mac os+selenium2+chrome驱动+python3
    mac 安装scrapy
    mac 查看系统位数
    ubuntu安装 mysqldb
    ubuntu安装常用软件
    scp 时出现permission denied
    ubuntu 安装git
  • 原文地址:https://www.cnblogs.com/hucaihao/p/3569658.html
Copyright © 2011-2022 走看看