Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > .NET для новичков > Regex для form html


Автор: aprok 13.2.2008, 19:51
не выходит у меня работать с регуляркой в С# smile 

нужна найти в html   все  параметры  формы  
например
Код


.....
<table width="100%" cellspacing="1" cellpadding="0">
<tr><td><label>Имя:</label>
<input class="fm fm110" type="text" name="e3fe509" value="123sdfa" maxlength="15"> <span class="e f7"></span>
</td></tr>

<tr><td><label>Пароль:</label>
<input class="fm fm110" type="password" name="e9a7f22" value="" maxlength="20"> <span class="e f7"></span>
.....

параметры value и name меняются динамически.....  я их  перед запросом штмл  не знаю.....

мне  нужна  после  обработки  получить массив  вида 
[0]
    [0]=e3fe509
    [1]=123sdfa
[1]
    [0]=password
    [1]=
.....

на Php  делал  очень легко 

Код



 $valre='/name\=\"(.*)\".*value\=\"(.*)[^\<^\s]*\"/'; 
 preg_match_all($valre,$login1['response'],$login_post,PREG_SET_ORDER);


а вот  в c#  НЕ ВЫХОДИТ ! =(  



Автор: Ctrl 14.2.2008, 15:28
Попробуй вот так (тут сама идея такого разбора):
Код

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace HtmlSimpleParse {
    public class HtmlHelper {
        static string GetAttributePattern(string attribute) {
            return attribute + "\\s*=\\s*[\"|\'](?<" + attribute + ">.*?)[\"|\']";
        }
        static string GetTagAttributesPattern(string tagName) {
            return "<"+tagName + "(?<attributes>.*?)>";
        }
        public static List<string> CollectTagAttributeStrings(string html, string tagName) {
            List<string> result = new List<string>();
            MatchCollection tagAttributesMatches = Regex.Matches(html, GetTagAttributesPattern(tagName));
            foreach(Match match in tagAttributesMatches) {
                string strTagAttributes = match.Groups["attributes"].Value;
                if(!string.IsNullOrEmpty(strTagAttributes)) result.Add(strTagAttributes);
            }
            return result;
        }
        public static string FindAttributeValue(string source, string attribute) {
            return Regex.Match(source, GetAttributePattern(attribute)).Groups[attribute].Value;
        }
    }
    class Program {
        static string html = "<table width=\"100%\" cellspacing=\"1\" cellpadding=\"0\">" +
                "<tr><td><label>Имя:</label>" +
                "<input class=\"fm fm110\" type=\"text\" name=\"e3fe509\" value=\"123sdfa\" maxlength=\"15\"> <span class=\"e f7\"></span>" +
                "</td></tr>" +
                "<tr><td><label>Пароль:</label>" +
                "<input class=\"fm fm110\" type=\"password\" name=\"e9a7f22\" value=\"\" maxlength=\"20\"> <span class=\"e f7\"></span>";
        static void Main(string[] args) {
            List<string> attributeStrings = HtmlHelper.CollectTagAttributeStrings(html, "input");
            foreach(string atrStr in attributeStrings) {
                Console.WriteLine("Class = " + HtmlHelper.FindAttributeValue(atrStr, "class"));
                Console.WriteLine("Type = " + HtmlHelper.FindAttributeValue(atrStr, "type"));
                Console.WriteLine("Name = " + HtmlHelper.FindAttributeValue(atrStr, "name"));
            }
        }
    }
}



Автор: aprok 14.2.2008, 16:58
СПАСИБ!!!!!
я  уже  легче сделал   , не так все трудно как я  думал  в C#   =)   smile 
Код

            Regex re = new Regex(@"name\=""(?<name>.*?)"".*value\=""(?<value>.*?)""", RegexOptions.IgnoreCase);
            MatchCollection mc = re.Matches(Client.html);


Автор: DenWPF 16.1.2010, 11:53
блин, делаю по этому же примеру и не получается=(

у меня такой вид     lj:user="e3fe509" надо вытащить e3fe509

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)