求一正则表单式

风淡云清2013 2016-10-14 04:11:30
想找出网页中含有 hidden 标签中的 name 和value

<input type="hidden" name="hf10" value="89038" />
<input type="hidden" name="hf20" value="B" />
<input type="hidden" name="phf222" value="对" /

 private List<string> GetElementContent(string content, string elementName, string subElement)
{

StringBuilder regexStr = new StringBuilder("(?is)<");
regexStr.Append(elementName).Append("[^>]*?").Append(subElement).Append(@"=(['""\s]?)([^'""\s]+)\1[^>]*?>");
Regex regex = new Regex(regexStr.ToString());
MatchCollection match = regex.Matches(content);
List<string> values = new List<string>();
foreach (Match m in match)
{
values.Add(m.Groups[2].Value);
}
return values;
}
//调用
List<string> values = GetElementContent(retString, "input '", "value"); //如果这样 正则为 {(?is)<input[^>]*?value=(['"\s]?)([^'"\s]+)\1[^>]*?>}
List<string> names = GetElementContent(retString, "input type='hidden'", "name"); //如果这样 正则为 /{(?is)<input type='hidden'[^>]*?name=(['"\s]?)([^'"\s]+)\1[^>]*?>}


...全文
96 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
EnForGrass 2016-10-14
  • 打赏
  • 举报
回复
引用 8 楼 stherix 的回复:
[quote=引用 7 楼 Chinajiyong 的回复:] [quote=引用 3 楼 u012048297 的回复:] {(?is)<input[^>]*?value=(['"\s]?)([^'"\s]+)\1[^>]*?>} 关键这句怎么改
为什么要分开呢?直接一步到位

 private static Dictionary<string,string> GetElementContent(string content, string elementName)
        {
            StringBuilder regexStr = new StringBuilder("(?is)<");
            regexStr.Append(elementName).Append("[^>]*?\\s+").Append(@"name\s*=\s*['""]([\w\W]+?)['""]\s+value\s*=\s*['""]([\w\W]+?)['""]\s*/>");
            Regex regex = new Regex(regexStr.ToString());
            MatchCollection match = regex.Matches(content);
            Dictionary<string, string> values = new Dictionary<string, string>();
            foreach (Match m in match)
            {
                values.Add(m.Groups[1].Value, m.Groups[2].Value);
            }
            return values;
        }

Dictionary<string, string> names = GetElementContent(htmlcontent, @"input\s*type\s*=\s*['""]hidden['""]");

            names.ToList().ForEach(x => Console.WriteLine("name:" + x.Key + ",value:" + x.Value));
[/quote] 这个是的啊 ,不过type name value之间也有可能有其他tag吧 光只有这3个属性倒是很好匹配 主要不知道是不是只有楼主那个例子那样的情况[/quote] 改一下就行了啊

  Dictionary<string, string> names = GetElementContent(htmlcontent, @"input\s*[^>]*?type\s*=\s*['""]hidden['""]");

   regexStr.Append(elementName).Append("[^>]*?\\s+").Append(@"name\s*=\s*['""]([\w\W]+?)['""]\s+[^>]*?value\s*=\s*['""]([\w\W]+?)['""][^>]*?/>");
stherix 2016-10-14
  • 打赏
  • 举报
回复
引用 7 楼 Chinajiyong 的回复:
[quote=引用 3 楼 u012048297 的回复:] {(?is)<input[^>]*?value=(['"\s]?)([^'"\s]+)\1[^>]*?>} 关键这句怎么改
为什么要分开呢?直接一步到位

 private static Dictionary<string,string> GetElementContent(string content, string elementName)
        {
            StringBuilder regexStr = new StringBuilder("(?is)<");
            regexStr.Append(elementName).Append("[^>]*?\\s+").Append(@"name\s*=\s*['""]([\w\W]+?)['""]\s+value\s*=\s*['""]([\w\W]+?)['""]\s*/>");
            Regex regex = new Regex(regexStr.ToString());
            MatchCollection match = regex.Matches(content);
            Dictionary<string, string> values = new Dictionary<string, string>();
            foreach (Match m in match)
            {
                values.Add(m.Groups[1].Value, m.Groups[2].Value);
            }
            return values;
        }

Dictionary<string, string> names = GetElementContent(htmlcontent, @"input\s*type\s*=\s*['""]hidden['""]");

            names.ToList().ForEach(x => Console.WriteLine("name:" + x.Key + ",value:" + x.Value));
[/quote] 这个是的啊 ,不过type name value之间也有可能有其他tag吧 光只有这3个属性倒是很好匹配 主要不知道是不是只有楼主那个例子那样的情况
EnForGrass 2016-10-14
  • 打赏
  • 举报
回复
引用 3 楼 u012048297 的回复:
{(?is)<input[^>]*?value=(['"\s]?)([^'"\s]+)\1[^>]*?>} 关键这句怎么改
为什么要分开呢?直接一步到位

 private static Dictionary<string,string> GetElementContent(string content, string elementName)
        {
            StringBuilder regexStr = new StringBuilder("(?is)<");
            regexStr.Append(elementName).Append("[^>]*?\\s+").Append(@"name\s*=\s*['""]([\w\W]+?)['""]\s+value\s*=\s*['""]([\w\W]+?)['""]\s*/>");
            Regex regex = new Regex(regexStr.ToString());
            MatchCollection match = regex.Matches(content);
            Dictionary<string, string> values = new Dictionary<string, string>();
            foreach (Match m in match)
            {
                values.Add(m.Groups[1].Value, m.Groups[2].Value);
            }
            return values;
        }

Dictionary<string, string> names = GetElementContent(htmlcontent, @"input\s*type\s*=\s*['""]hidden['""]");

            names.ToList().ForEach(x => Console.WriteLine("name:" + x.Key + ",value:" + x.Value));
stherix 2016-10-14
  • 打赏
  • 举报
回复
引用 2 楼 stherix 的回复:
以我的正则能力是写不出来 可以考虑其他方法啊 比如jquery(外部页面是没办法了) 比如利用DOM (webbroswer的话) 比如xml遍历查找
单你这个例子的话 <input type="hidden".*name="([^"]+)" 可以找出name <input type="hidden".*value="([^"]+)" 可以找出value
EnForGrass 2016-10-14
  • 打赏
  • 举报
回复
或者

   var htmlcontent = @"<input type=""hidden"" name=""hf10"" value=""89038""  />fdasfdsa<input type = ""hidden"" name = ""hf20"" value = ""B"" /><input type = ""hidden"" name = ""phf222"" value = ""对"" /> ";
            foreach (Match reg in Regex.Matches(htmlcontent, @"(?is)<input\s+type\s*=\s*['""]hidden['""]\s+name\s*=\s*['""]([\w\W]+?)['""]\s+value\s*=\s*['""]([\w\W]+?)['""]\s*/>"))
            {
               // Console.WriteLine(reg.Value);
                Console.WriteLine("name:"+reg.Groups[1].Value + ",value:"+ reg.Groups[2].Value);
            }
EnForGrass 2016-10-14
  • 打赏
  • 举报
回复

  private static List<string> GetElementContent(string content, string elementName, string subElement)
        {
            StringBuilder regexStr = new StringBuilder("(?is)<");
            regexStr.Append(elementName).Append("[^>]*?\\s+").Append(subElement).Append(@"\s*=\s*['""]([\w\W]+?)['""]\s+value\s*=\s*['""]([\w\W]+?)['""]\s*/>");
            Regex regex = new Regex(regexStr.ToString());
            MatchCollection match = regex.Matches(content);
            List<string> values = new List<string>();
            foreach (Match m in match)
            {
                values.Add(m.Groups[1].Value);
            }
            return values;
        }

List<string> names = GetElementContent(retString, @"input\s*type\s*=\s*['""]hidden['""]", "name");

            names.ForEach(x => Console.WriteLine(x));
风淡云清2013 2016-10-14
  • 打赏
  • 举报
回复
{(?is)<input[^>]*?value=(['"\s]?)([^'"\s]+)\1[^>]*?>} 关键这句怎么改
stherix 2016-10-14
  • 打赏
  • 举报
回复
以我的正则能力是写不出来 可以考虑其他方法啊 比如jquery(外部页面是没办法了) 比如利用DOM (webbroswer的话) 比如xml遍历查找
风淡云清2013 2016-10-14
  • 打赏
  • 举报
回复
List<string> values = GetElementContent(retString, "input", "value");
List<string> names = GetElementContent(retString, "input type='hidden'", "name");

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧