C# 提取ID并替换“示例HTML”中的所有内容`
对于正则表达式,我希望在HTML中包含以下文本,并希望替换为其他文本 HTML示例:C# 提取ID并替换“示例HTML”中的所有内容`,c#,regex,replace,C#,Regex,Replace,对于正则表达式,我希望在HTML中包含以下文本,并希望替换为其他文本 HTML示例: {{Object id='foo'}} string strStart = "Object"; string strFind = "{{(" + strStart + ".*?)}}"; Regex regExp = new Regex(strFind, RegexOptions.IgnoreCase); Match matchRegExp = regExp.Match(html); while (mat
{{Object id='foo'}}
string strStart = "Object";
string strFind = "{{(" + strStart + ".*?)}}";
Regex regExp = new Regex(strFind, RegexOptions.IgnoreCase);
Match matchRegExp = regExp.Match(html);
while (matchRegExp.Success)
{
//At this point, I have this variable:
//{{Object id='foo'}}
//I can find the id='foo' (see below)
//but not sure how to extract 'foo' and use it
string strFindInner = "id='(.*?)'"; //"{{Slider";
Regex regExpInner = new Regex(strFindInner, RegexOptions.IgnoreCase);
Match matchRegExpInner = regExpInner.Match(matchRegExp.Value.ToString());
//Do something with 'foo'
matchRegExp = matchRegExp.NextMatch();
}
将id提取到一个变量中,如下所示:
string strId = "foo";
到目前为止,我有以下正则表达式代码将捕获示例HTML:
{{Object id='foo'}}
string strStart = "Object";
string strFind = "{{(" + strStart + ".*?)}}";
Regex regExp = new Regex(strFind, RegexOptions.IgnoreCase);
Match matchRegExp = regExp.Match(html);
while (matchRegExp.Success)
{
//At this point, I have this variable:
//{{Object id='foo'}}
//I can find the id='foo' (see below)
//but not sure how to extract 'foo' and use it
string strFindInner = "id='(.*?)'"; //"{{Slider";
Regex regExpInner = new Regex(strFindInner, RegexOptions.IgnoreCase);
Match matchRegExpInner = regExpInner.Match(matchRegExp.Value.ToString());
//Do something with 'foo'
matchRegExp = matchRegExp.NextMatch();
}
我知道这可能是一个简单的解决方案,我希望获得更多关于正则表达式的知识,但更重要的是,我希望收到一个关于如何更干净、更有效地处理此问题的建议
多谢各位
编辑:
这是一个我可能使用的示例:虽然我没有用正则表达式解决我的初始问题,但我确实使用了
子字符串
、索引和字符串.Split
暂时使用了一个更简单的解决方案,我知道我的代码需要清理,但我想我会发布到目前为止的答案
string html = "<p>Start of Example</p>{{Object id='foo'}}<p>End of example</p>"
string strObject = "Slider"; //Example
//When found, this will contain "{{Object id='foo'}}"
string strCode = "";
//ie: "id='foo'"
string strCodeInner = "";
//Tags will be a list, but in this example, only "id='foo'"
string[] tags = { };
//Looking for the following "{{Object "
string strFindStart = "{{" + strObject + " ";
int intFindStart = html.IndexOf(strFindStart);
//Then ending in the following
string strFindEnd = "}}";
int intFindEnd = html.IndexOf(strFindEnd) + strFindEnd.Length;
//Must find both Start and End conditions
if (intFindStart != -1 && intFindEnd != -1)
{
strCode = html.Substring(intFindStart, intFindEnd - intFindStart);
//Remove Start and End
strCodeInner = strCode.Replace(strFindStart, "").Replace(strFindEnd, "");
//Split by spaces, this needs to be improved if more than IDs are to be used
//but for proof of concept this is perfect
tags = strCodeInner.Split(new char[] { ' ' });
}
Dictionary<string, string> dictTags = new Dictionary<string, string>();
foreach (string tag in tags)
{
string[] tagSplit = tag.Split(new char[] { '=' });
dictTags.Add(tagSplit[0], tagSplit[1].Replace("'", "").Replace("\"", ""));
}
//At this point, I can replace "{{Object id='foo'}}" with anything I'd like
//What I don't show is that I go into the website's database,
//get the object (ie: Slider) and return the html for slider with the ID of foo
html = html.Replace(strCode, strView);
/*
"html" variable may contain:
<p>Start of Example</p>
<p id="foo">This is the replacement text</p>
<p>End of example</p>
*/
string html=“示例开始{{Object id='foo'}}示例结束”
string strObject=“Slider”//例子
//找到时,它将包含“{Object id='foo'}”
字符串strCode=“”;
//ie:“id='foo'”
字符串strCodeInner=“”;
//标记将是一个列表,但在本例中,只有“id='foo'”
字符串[]标记={};
//正在查找以下“{Object”
字符串strFindStart=“{{{”+strObject+”;
int intFindStart=html.IndexOf(strFindStart);
//然后在下面结束
字符串strFindEnd=“}}”;
int intFindEnd=html.IndexOf(strFindEnd)+strFindEnd.Length;
//必须找到开始和结束条件
if(intFindStart!=-1&&intFindEnd!=-1)
{
strCode=html.Substring(intFindStart,intFindEnd-intFindStart);
//删除开始和结束
strCodeInner=strCode.Replace(strFindStart,“”)。Replace(strFindEnd,“”);
//按空格分割,如果要使用多个ID,这需要改进
//但对于概念证明来说,这是完美的
tags=strCodeInner.Split(新字符[]{''});
}
Dictionary dictTags=新字典();
foreach(标记中的字符串标记)
{
字符串[]tagSplit=tag.Split(新字符[]{'='});
dictTags.Add(tagSplit[0],tagSplit[1]。替换(“,”)。替换(“,”);
}
//在这一点上,我可以用我喜欢的任何东西替换“{objectid='foo'}”
//我没有显示的是我进入了网站的数据库,
//获取对象(即:Slider)并返回ID为foo的Slider的html
html=html.Replace(strCode,strView);
/*
“html”变量可能包含:
开始举例
这是替换文本
示例结束
*/
停下来!看一看,听一听!每天都有人会想到用正则表达式解析Html的好主意。没有什么比Xml解析器更能解析Html了。虽然你问问题的方式可能掩盖了这有多难!使用{{
而不是
可以隐藏解析“>\uo”这样的注释的事实/可以把你的正则表达式变成一场噩梦。在你的头脑中,正则表达式是一个简单的“寻找这个”不是的!解析html正则表达式必须重复,每次都要从头开始。使用解析器,你的代码就会像在js中一样简单。谢谢你,我很重视你的观点,正则表达式似乎是一种简单的方法,但似乎不是。我试图在尝试做一些事情时,尝试进入子字符串和索引与WordPress的doShortCode()类似完成并能够找到关于当前如何工作的文档。我希望获得概念证明并从那里继续。使用Html解析器作为。一个简单的nuget和bim,您可以在Html中选择任何您想要的内容。学习并不难,几乎没有什么可学。要获得概念证明,请使用一些关键字和Google search不要在非网站资源列表中回答这个问题。每个解析html的图书馆都在主页上有很好的例子。解析html非常常见,你可以在任何地方找到freelib。有趣的是,每个人都建议使用html agility pack…然而在StackOverflow的10年里,我只看到一个人回答一个问题这是一个正则表达式问题,所以你的里程数可能会有所不同。