问题描述
奇怪的问题,但是我不会浪费时间解释为什么我需要这样做,只是我需要这样做.
Strange question, but I won't waste time explaining why I need to do this, just that I need to do it.
我有以下内容:
<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 />
我需要将其转换为:
<button type="button" name="eq_9" id="eq_9_2" onclick="nextStepID_load('912');">Installer</button><br />
我正在使用C#/asp.net(3.5或更低版本),而javascript则是performJS(在我弄清楚如何替换html之前,它是一个占位符).
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.
现在,我尝试添加.Replace(","\">);确实替换了单选标记,但显然使它在代码方面看起来很恐怖,并且不会删除标签或放置在标签之间标记内容.
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.
我敢肯定,用正则表达式可以最好地解决这个问题,但是我对正则表达式不是很熟悉.我一直在与regexlib玩弄,看看我是否可以自己搞定一个regex ...这是我到目前为止的成果,尽管我想我还很遥远.
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 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 />
如您所见,我已经参数化了 X , Y , Z 和 N .以下是我对您的疑问:
As you see, I've parameterized X, Y, Z, and N. Here are questions I have for you:
-
您能说我对您的问题的参数化可以准确地描述它吗?
Would you say my parameterization of your problem describes it accurately?
这是否在某个特定的DTD下验证?如果是,则验证哪个?
Does this validate under some particular DTD, and if so, which one?
这些属性是否始终都是其他属性?
Are the attributes always none other than those?
这些属性是否总是以这种精确顺序出现?
Are the attributes that occur always in that precise order?
您必须执行多少此类操作?
How many such things do you have to do?
这是在纯HTML中发生的,还是实际上隐藏在某些Javascript中?
Does this occur in plain HTML, or is it actually hidden in some Javascript?
您是否知道是否有任何< script>
或< style>
元素或<!-...->
注释包含的内容恰好符合您的需求?
Do you know whether there are any <script>
or <style>
elements, or <!-- ... -->
comments that contain things that look just what you are looking for?
您是否知道要寻找的事物中间是否存在任何此类元素?
Do you know whether there are any such elements intervening in the middle of the thing you are looking for?
是否所有形式为 NAME ="VALUE"
的属性的值周围都只有双引号,而不是单引号或完全省略?
Are all the attributes of the form NAME="VALUE"
with only double quotes around the value, never single quotes or omitted altogether?
标识符的大小写是否总是小写?
Is the casing of the identifiers always in lower case?
它们全部都在一个文件中吗?
Are they all in one file?
是否有某些原因导致您的输出样本丢失了一些不重要的空格?
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:
-
您以前曾经使用过HTML解析类吗?
Have you ever used an HTML parsing class before?
您想学习如何吗?
这篇关于将输入类型设为“收音机".进入& lt; button& gt;使用Regex/C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!