English 中文(简体)
A. 投入方的名称和价值
原标题:Get name and value from the input tag
  • 时间:2010-05-28 04:09:17
  •  标签:
  • html
  • regex

在你说我的案件之前,我在这里说的是“不要再说”。 I m parsing part of RUS production and the only what I m interest in is and Value characteristics of each <input/> tag. 超文本实际上是超文本碎块,可能不知情。 我没有电离层或超音层,我没有试图在任何方面放下nes子。 问题在于,我不了解属性的命令或数量,因此它可以是:<input name=“foo” Value=“boo”/><input category=“hidden” name=foo>或<input id=blah Value= boo src='image.png” name=“foo”e=“img”/>

是否有一个固定的表述,能够使我得到<条码><>> 姓名/编码>和<条码> 价值 等值,以可预见的顺序排列? 我不想问,我是否可以假设<代码> 姓名/代码>的归属总是在<代码>> 数值/代码>之前,但不幸的是,情况并非如此。

最佳回答

为将<代码> 姓名/代码>和<编码> > 数值输入同一组,不论有无次序,你均可尝试

<input (?=[^>]* name=[" ]([^ "]*)|)(?=[^>]* value=[" ]([^ "]*)|)

if your regex implementation supports lookaheads. This assumes that the values are quoted.

问题回答

这里使用一种解决办法。 NET 定期表达辛奈

var regex = new Regex(@"
        <input
            (
                s*
                (?<name>[^=]+)
                =
                ([ ""])
                (?<value>.*?)
                2
            )*
        s*/?>
    ", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);

foreach(Match m in regex.Matches(input))
{
    var names = m.Groups["name"];
    var values = m.Groups["value"];

    for(int i = 0; i < names.Captures.Count; i++)
    {
        Console.WriteLine("Name = {0} Value = {1}",
                names.Captures[i].Value, values.Captures[i].Value);
    }
}

投入如:

blah blah blah <input name=“hi”=“world”= foo /> blah blah

产出:

Name = name Value = hi
Name = value Value = world
Name = test Value = foo

它处理的是<代码> 名称=数值(即不援引价值),但这不应太难增加支持。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签