【问题标题】:Extract ID and replace everything in `Example HTML`提取 ID 并替换“示例 HTML”中的所有内容
【发布时间】:2017-08-21 08:45:15
【问题描述】:

正则表达式新手,我想在我的 HTML 中包含以下文本,并想用其他内容替换

示例 HTML:

{{Object id='foo'}}

将 id 提取到这样的变量中:

string strId = "foo";

到目前为止,我有以下将捕获示例 HTML 的正则表达式代码:

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

我知道这可能是一个简单的解决方案,我希望获得更多关于正则表达式的知识,但更重要的是,我希望收到有关如何更清洁、更有效地处理这个问题的建议。

谢谢

编辑:

这是我可能使用的示例吗:c# regex replace

【问题讨论】:

  • 停下!一边看一边听!每天都有人醒来时想到了用正则表达式解析 Html 的好主意。没有什么比 Xml 解析器更好的解析 Html 了。虽然您提出问题的方式可能会掩盖问题的难度!使用{{ 而不是<> 可以隐藏这样一个事实,即解析像 ">_ _o/" 这样的评论会使你的正则表达式变成一场噩梦。在你的头脑中,正则表达式是一个简单的“寻找这个”它不是!要解析 html 正则表达式,每次都必须重复并回到开头。使用解析器,您的代码将像在 js 中一样简单。
  • 谢谢您,我重视您的意见,RegEx 似乎是一种简单的方法,但似乎并非如此。我试图进入SubStringIndexOf,因为我正在尝试做类似于 WordPress 的 doShortCode() 完成的事情,并且能够找到有关当前工作方式的文档。我希望获得概念验证并从那里继续前进。
  • 使用 Html 解析器作为 Html Agility Pack (HAP)。一个简单的 nuget 和 bim,你可以在 html 中选择任何你想要的东西。学习并不难,几乎没有什么可学的。
  • 要获得概念证明,请使用一些关键字和 Google 搜索,不要将此问题置于站点外资源列表中。每个解析 html 的库在主页上都有强大的示例。而且解析 html 是如此普遍,你可以在任何地方找到 freelib。
  • 有趣的是,每个人都建议使用 HTML 敏捷包……然而在 StackOverflow 上的 10 年里,我只看到一个人在正则表​​达式问题上用它回答问题。所以你的里程可能会有所不同。

标签: c# regex replace


【解决方案1】:

虽然我没有用正则表达式解决我最初的问题,但我确实暂时使用SubStringIndexOfstring.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>

*/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-08
    • 2015-05-03
    • 1970-01-01
    • 2023-03-28
    • 2014-06-07
    • 2012-10-28
    • 2018-07-05
    • 1970-01-01
    相关资源
    最近更新 更多