开发者

Turning a <input type=radio> into a <button> with Regex/C#

开发者 https://www.devze.com 2023-01-26 20:20 出处:网络
Strange question, but I won\'t waste time explaining why I need to do this, just that I need to do it.

Strange question, but I won't waste time explaining why I need to do this, just that I need to do it.

开发者_如何学JAVA

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 that into:

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

I am using C#/asp.net (3.5 or below), and javascript for the performJS(which is a placeholder until I figure out how to replace the html).

Please note, the source providing this is sending me a string with the MANY rows of the inputs. And I need to replace each row with the info that is valid for it.

Right now, I've tried adding a .Replace("","\">"); which does replace the radio tags, but obviously makes it look horrible codewise, and doesn't remove the label or put the label contents in between the tags.

I'm sure this is probably best solved by a regex, but I'm not very familiar with regex. I have been toying with regexlib to see if I can figure out a regex on my own... here is what I have so far, although I imagine I'm pretty far off.

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);


Do not use regular expressions to work with HTML. It is only flexible enough for 95% of all cases, and that should tell you that it is the wrong tool for the job.

Using the HTML Agility Pack, you can load in your document and use something like this to replace...

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
    }
}


I'm sure this is probably best solved by a regex, but I'm not very familiar with regex.

I'm afraid that is not a good sign, since if you are not very familiar with regexes, then it is rather unlikely that anything is best solved by a regex. :(

There isn't enough description of the problem to know for sure whether even a regex wizard could quickly craft a solution using regexes. I am pretty sure you need to do more than merely exchange one fixed string for another, because if you did, you'd've done that already. So parts of it must be parameterized. I just don't know which.

Not counting whitespace, would you say that your problem is one of transforming input of this form:

<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 />

into output of this form:

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

<br />

As you see, I've parameterized X, Y, Z, and N. Here are questions I have for you:

  1. Would you say my parameterization of your problem describes it accurately?

  2. Does this validate under some particular DTD, and if so, which one?

  3. Are the attributes always none other than those?

  4. Are the attributes that occur always in that precise order?

  5. How many such things do you have to do?

  6. Does this occur in plain HTML, or is it actually hidden in some Javascript?

  7. Do you know whether there are any <script> or <style> elements, or <!-- ... --> comments that contain things that look just what you are looking for?

  8. Do you know whether there are any such elements intervening in the middle of the thing you are looking for?

  9. Are all the attributes of the form NAME="VALUE" with only double quotes around the value, never single quotes or omitted altogether?

  10. Is the casing of the identifiers always in lower case?

  11. Are they all in one file?

  12. Is there some reason that your output sample lost some of its non-significant whitespace?

It is questions like these that show why the problem is almost certainly much more complicated than it appears — which begs a couple of final questions:

  1. Have you ever used an HTML parsing class before?

  2. Would you like to learn how?


this exercise does what you need utilizing regular expressions.

This is how it works: I do not replace values in the original string. Instead, I take the goal string, where we want to arrive to, and build it with the correct values. I believe that this approach will give you flexibility, you can format the output string however you want.

  • The inputList is a list of test cases.
  • The target is the goal string with placeholders, we can format it however we want/need/have to.
  • The GetValue() method utilizes the regex argument to seek for an specific value. It finds the html tag as a KeyValue pair, it takes the Value and removes the enclosing quotes.
  • Finally with the string.Format() we build the output string as desired.

It is full code, so you can try it. You can also turn the idea of this piece of code into a method an integrate it in your solution.

Please let me know if it worked for you too.

      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);
    }

this is the input:

    <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 />

this is the output:

    <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 />
0

精彩评论

暂无评论...
验证码 换一张
取 消