【问题标题】:How to replace input text tag into plain text of it's value using C# regex?如何使用 C# 正则表达式将输入文本标签替换为其值的纯文本?
【发布时间】:2014-10-10 08:09:29
【问题描述】:

我有像

这样的 html 字符串
<td AutoTab="true" Compulsory="true" ValidationGroup="OU20141008-0001" class="bn_TextBox bn_TextBox_com i" class="r">
                <span id="FixedGrid1__txtSUPPLY_AMT_0_Container">
                <input AutoTab="true" Compulsory="true" ValidationGroup="OU20141008-0001" class="bn_TextBox bn_TextBox_com r ime-disabled" name="FixedGrid1$ctl11$_txtSUPPLY_AMT" type="text" value="900000" readonly="readonly" id="FixedGrid1__txtSUPPLY_AMT_0" style="color:0;background-color:0;width:96%;" />

                </span>
            </td><td class="r">
                <span id="FixedGrid1__txtM_AMT_0_Container"><input AutoTab="true" class="bn_TextBox r ime-disabled" name="FixedGrid1$ctl11$_txtM_AMT" type="text" value="818181" readonly="readonly" id="FixedGrid1__txtM_AMT_0" style="color:0;background-color:0;width:96%;" /></span>
            </td><td class="r">
                <span id="FixedGrid1__txtTAX_AMT_0_Container"><input AutoTab="true" Compulsory="true" ValidationGroup="OU20141008-0001" class="bn_TextBox bn_TextBox_com r ime-disabled" name="FixedGrid1$ctl11$_txtTAX_AMT" type="text" value="81818" readonly="readonly" id="FixedGrid1__txtTAX_AMT_0" style="color:0;background-color:0;width:96%;" /></span>
            </td><td class="c">2014-10-08</td><td>1111</td><td class="c">
                <span id="FixedGrid1_Label5_0_Container"><span id="FixedGrid1_Label5_0" class="bn_Label">2014-10-08</span></span>

            </td><td class="c">
                <span id="FixedGrid1_Label6_0_Container"><span id="FixedGrid1_Label6_0" class="bn_Label">2014-10-08</span></span>

            </td>

我需要将这个 input type=text html 标签替换为它的值的纯文本。

喜欢

<input AutoTab="true" Compulsory="true" ValidationGroup="OU20141008-0001" class="bn_TextBox bn_TextBox_com r ime-disabled" name="FixedGrid1$ctl11$_txtSUPPLY_AMT" type="text" value="900000" readonly="readonly" id="FixedGrid1__txtSUPPLY_AMT_0" style="color:0;background-color:0;width:96%;" />

这仅保留在 900000

我知道有 HTML Agility 包,它简单快捷。

但在这种情况下,我不能使用任何 3rd 方库。

有人可以帮忙吗?

【问题讨论】:

    标签: c# html regex


    【解决方案1】:

    您需要匹配整个输入,最好按名称匹配,因为这应该是唯一标记,但要捕获要在替换中使用的 value 属性。如果您使用ExplicitCapture,这会更简单一些,因此您可以在替换中使用捕获的名称 - 请参阅Substituting a Named Group on MSDN

    总结正则表达式:

    1. \&lt;input - 匹配标签的开头
    2. .*? - 匹配可能到达下一位的最少字符(名称属性)
    3. name=""FixedGrid1\$ctl11\$_txtSUPPLY_AMT"" - 匹配名称标签
    4. .*? - 匹配可能到达下一位的最少字符(值属性)
    5. value=""(?&lt;val&gt;[^""]*)"" - 匹配 value 属性并在 val 中捕获其值
    6. .*? - 匹配可能到达下一位(标签末尾)的最少字符
    7. \&gt; - 匹配标签的结尾

    Regex.Replace(input, @"\&lt;input.*?name=""FixedGrid1\$ctl11\$_txtSUPPLY_AMT"".*?value=""(?&lt;val&gt;[^""]*)"".*?\&gt;", "${val}", RegexOptions.ExplicitCapture);

    【讨论】:

    • 感谢您的回答。我将您的代码更改为 Regex.Replace(html, @"\[^""]*)"".*?\>", "${val}" , RegexOptions.ExplicitCapture)。因为,我必须找到所有输入文本标签。
    • 好,这也应该绕过 namevalue 属性,相反。只要标签中没有任何随机的&gt;s 就可以了
    • 再次感谢您的友好解释。
    【解决方案2】:

    以下正则表达式应该这样做(至少对于给定的示例):

    <input.+(type="text"|value="(?<Val>[^"]+)").*/>
    

    其背后的逻辑如下:

    1. 匹配所有input标签
    2. 具有type=textvalue=&lt;something&gt; 属性,无论它们出现的顺序如何
    3. 在命名组Val 中捕获value 属性中的字符串
    4. 匹配input 标记的其余部分。

    现在,只需使用以下 sn-p 替换即可:

    string value = @"${Val}";
    return myRegex.Replace(targetString, value);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      相关资源
      最近更新 更多