谈到A<输入类型=广播GT;成<按钮和GT;与正则表达式/ C# [英] Turning a <input type=radio> into a <button> with Regex/C#

查看:109
本文介绍了谈到A<输入类型=广播GT;成<按钮和GT;与正则表达式/ C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

奇怪的问题,但为什么我需要这样做,只是我需要做的,我不会浪费时间解释。

我有以下内容:

 <输入类型=电台NAME =eq_9ID =eq_9_2的onclick =nextStepID_load(本);值=安装程序。标题=912/><标签=eq_9_2>安装< /标签> < BR />

我需要将其转换成:

 <按钮式=按钮NAME =eq_9ID =eq_9_2的onclick =nextStepID_load('912');>安装< /按钮>< BR />

我使用C#/ asp.net(3.5或以下)和JavaScript的performJS(这是一个占位符,直到我弄清楚如何更换HTML)。

请注意,这提供源发送我输入的多行的字符串。而我需要的是有效的它的信息,以取代每一行。

现在,我已经尝试添加一个.Replace(,\\>);它不取代无线标签,但显然使得它看起来可怕codewise,并且不会删除标签或者将标签内容在标签之间。

我敢肯定,这可能是由一个正则表达式最好的解决,但我不是很熟悉的正则表达式。我一直在玩弄regexlib看看,如果我可以对我自己想出一个正则表达式......这里是我到目前为止,虽然我想象我是pretty不远了。

 字符串strRegex ​​= @<输入类型=?单选,[\\ S] * = \\ s] *[^ \\ w _] *? [^]的计算值*>中;
RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline;
正则表达式myRegex ​​=新的正则表达式(strRegex,myRegexOptions);
字符串strTargetString = @<输入类型=单选,NAME =eq_9ID =eq_9_2的onclick =nextStepID_load(本);VALUE =安装程序,标题= 912/><标签=eq_9_2>安装< /标签>< BR />中;
字符串strReplace =<按钮式=按钮>< /按钮>中;
返回myRegex.Replace(strTargetString,strReplace);


解决方案

  

我敢肯定,这可能是最好的一个正则表达式解决,但我不是很熟悉的正则表达式。


恐怕不是一个好兆头,因为如果你是不是很熟悉正则表达式,那么它是相当不可能的任何的是最好的正则表达式解决。 (

有是没有问题的描述不够肯定知道是否使用正则表达式,即使一个正则表达式向导,可以快速制作一个解决方案。我是pretty确定你需要做的更多,而不仅仅是另交换一个固定的字符串,因为如果你这样做,you'd've做到这一点了。所以,它的部分必须进行参数设置。我只是不知道是哪个。

不计空格,你会说,你的问题是把这种形式的输入之一:

 <输入
    类型=无线电
    NAME =eq_X
    ID =eq_X_Y
    的onclick =nextStepID_load('XNY');
    值=Z。
    标题=XNY
/>
<标签=eq_X_Y> z,其中; /标签>
< BR />

成这种形式的输出:

 <按钮
    类型=按钮
    NAME =eq_X
    ID =eq_X_Y
    的onclick =nextStepID_load('XNY');
>
ž
< /按钮>< BR />

正如你所见,我已经参数的 X 以Z N 的。这里是你的问题我有:


  1. 你会说我你的问题的参数准确地描述它?


  2. 下是否某些特定的DTD这个验证,如果是这样,哪一个?


  3. 是属性始终没有比其他?


  4. 是在precise秩序总是会发生的属性?


  5. 有多少这样的事情,你要干什么?


  6. 这是否发生在纯HTML,或者它实际上隐藏在一些JavaScript?


  7. 你知道是否存在任何<脚本> <风格> 元素,或<! - ... - > 包含的东西,看起来你在寻找的地方征求意见


  8. 你知道是否有你正在寻找的东西中间插入的任何这样的元素?


  9. 的形式的所有属性NAME =值与周围的值只有双引号,从来没有单引号或完全忽略?


  10. 是标识符的外壳总是小写?


  11. 他们都是在一个文件?


  12. 有一些原因,你的输出样本失去了一些非显著空白?


有这样的,显示问题,为什么这个问题是几乎可以肯定的要复杂得多比它出现 - 这引出一对夫妇的最后问题:


  1. 你以前使用的HTML解析类?


  2. 你想学习怎么样?


Strange question, but I won't waste time explaining why I need to do this, just that I need 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 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);

解决方案

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?

这篇关于谈到A&LT;输入类型=广播GT;成&LT;按钮和GT;与正则表达式/ C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆