Including <input type = radio> in <button> using Regex / C #

A strange question, but I will not waste time explaining why I need to do this, just to do it.

I have the following:

<input type="radio" name="eq_9" id="eq_9_2"  onclick="nextStepID_load(this);" value="Installer." title="912" /><label for="eq_9_2">Installer</label> <br />

I need to turn this into:

<button type="button" name="eq_9" id="eq_9_2" onclick="nextStepID_load('912');">Installer</button><br />

I use C # / asp.net (3.5 or lower) and javascript for executeJS (which is a placeholder until I figure out how to replace html).

Note: the source providing this sends me a line with MANY input lines. And I need to replace each line with information that is valid for her.

Currently, I have tried adding .Replace ("," \ ">"), which replaces the radio libraries, but obviously makes it look awful in code and does not remove the shortcut or put the contents of the shortcuts between the tags.

I'm sure this is probably best resolved by regex, but I'm not very familiar with regex. I played with regexlib to find out if I can find the regex myself ... that's what I have so far, although I think I'm pretty far away.

string strRegex = @"<input type=""radio"" [\s]*=[\s]*""?[^\w_]*""?[^>]*>";
RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"<input type=""radio"" name=""eq_9"" id=""eq_9_2""  onclick=""nextStepID_load(this);"" value=""Installer."" title=""912"" /><label for=""eq_9_2"">Installer</label> <br />";
string strReplace = "<button type="button"></button>";
return myRegex.Replace(strTargetString, strReplace);
0
source share
3 answers

I am sure that this probably solves the regex best, but I am not very familiar with the regex.

I'm afraid this is not a good sign, because if you are not very familiar with regular expressions, then it is unlikely that anything solves the regular expression best. :(

, , . , , , , . . , .

, , :

<input 
    type="radio"
    name="eq_X"
    id="eq_X_Y"
    onclick="nextStepID_load('XNY');"
    value="Z."
    title="XNY"  
/>
<label for="eq_X_Y"> Z </label>
<br />

:

<button 
    type="button" 
    name="eq_X" 
    id="eq_X_Y" 
    onclick="nextStepID_load('XNY');" 
>
Z
</button>

<br />

, X, Y, Z N. , :

  • , ?

  • DTD, , ?

  • ?

  • , ?

  • ?

  • HTML, Javascript?

  • , - <script> <style>, <!-- ... --> , , , ?

  • , - , ?

  • NAME="VALUE" , ?

  • ?

  • ?

  • - , ?

, , , , :

  • - HTML?

  • , ?

+2

HTML. 95% , , .

HTML Agility Pack, - ...

HtmlDocument doc = new HtmlDocument();
doc.Load(@"C:\Path\To\Page.html");

HtmlNode radios = doc.SelectNodes("//input[@type=radio]");

foreach (HtmlNode node in radios)
{
    HtmlAttribute name = node.Attributes["name"];

    if (name != null && name.ToLower().StartsWith("eq_"))
    {
        //Build your button element and replace the radio using ReplaceChild
    }
}
+4

, , .

: . , , . , , , .

  • InputList - .
  • - , , //.
  • GetValue() regex . html- KeyValue, .
  • , string.Format() .

, . , .

, , .

      static void Main(string[] args)
    {

        List<string> inputList = new List<string>();
        inputList.Add("<input type=\"radio\" name=\"eq_9\" id=\"eq_9_2\"  onclick=\"nextStepID_load(this);\" value=\"Installer.\" title=\"912\" /><label for=\"eq_9_2\">Installer</label> <br />");
        inputList.Add("<input type=\"radio\" name=\"eq_10\" id=\"eq_9_3\"  onclick=\"nextStepID_load(this);\" value=\"Installer1.\" title=\"913\" /><label for=\"eq_9_3\">InstallerA</label> <br />");
        inputList.Add("<input type=\"radio\" name=\"eq_11\" id=\"eq_9_4\"  onclick=\"nextStepID_load(this);\" value=\"Installer2.\" title=\"914\" /><label for=\"eq_9_4\">InstallerB</label> <br />");
        inputList.Add("<input type=\"radio\" name=\"eq_12\" id=\"eq_9_5\"  onclick=\"nextStepID_load(this);\" value=\"Installer3.\" title=\"915\" /><label for=\"eq_9_5\">InstallerC</label> <br />");
        inputList.Add("<input type=\"radio\" name=\"eq_13\" id=\"eq_9_6\"  onclick=\"nextStepID_load(this);\" value=\"Installer4.\" title=\"916\" /><label for=\"eq_9_6\">InstallerD</label> <br />");

        string output = string.Empty;
        string target = "<button type=\"button\" name=\"{0}\" id=\"{1}\" onclick=\"nextStepID_load('{2}');\">{3}</button><br />";

        foreach (string input in inputList)
        {
            string name = GetValue(@"(?<Value>name=[\S]+)", input);
            string id = GetValue(@"(?<Value>id=[\S]+)", input);
            string title = GetValue(@"(?<Value>title=[\S]+)", input);
            string value = GetValue(@"(?<Value>value=[\S]+)", input);

            output = string.Format(target, name, id, title, value);
            System.Diagnostics.Debug.WriteLine(output);
        }

    }

    private static string GetValue(string pattern, string input)
    {
        Regex regex = new Regex(pattern);
        Match match = regex.Match(input);
        return match.ToString().Split('=').Last().Replace("\"", string.Empty);
    }

:

    <input type="radio" name="eq_9" id="eq_9_2"  onclick="nextStepID_load(this);" value="Installer." title="912" /><label for="eq_9_2">Installer</label> <br />
    <input type="radio" name="eq_10" id="eq_9_3"  onclick="nextStepID_load(this);" value="Installer1." title="913" /><label for="eq_9_3">InstallerA</label> <br />
    <input type="radio" name="eq_11" id="eq_9_4"  onclick="nextStepID_load(this);" value="Installer2." title="914" /><label for="eq_9_4">InstallerB</label> <br />
    <input type="radio" name="eq_12" id="eq_9_5"  onclick="nextStepID_load(this);" value="Installer3." title="915" /><label for="eq_9_5">InstallerC</label> <br />
    <input type="radio" name="eq_13" id="eq_9_6"  onclick="nextStepID_load(this);" value="Installer4." title="916" /><label for="eq_9_6">InstallerD</label> <br />

:

    <button type="button" name="eq_9" id="eq_9_2" onclick="nextStepID_load('912');">Installer.</button><br />
    <button type="button" name="eq_10" id="eq_9_3" onclick="nextStepID_load('913');">Installer1.</button><br />
    <button type="button" name="eq_11" id="eq_9_4" onclick="nextStepID_load('914');">Installer2.</button><br />
    <button type="button" name="eq_12" id="eq_9_5" onclick="nextStepID_load('915');">Installer3.</button><br />
    <button type="button" name="eq_13" id="eq_9_6" onclick="nextStepID_load('916');">Installer4.</button><br />
+1
source

Source: https://habr.com/ru/post/1786024/


All Articles