Как мне решить проблему ниже?
Я создаю простую систему управления контентом, в которой есть шаблон HTML со специальной разметкой, которая обозначает, где должен быть контент:
<html><head></head><body><!-- #Editable "Body1" --><p>etc etc</p><!-- #Editable "Extra" --></body></html>
Помимо этого, в поле базы данных есть содержимое, которое выглядит примерно так:
<!-- #BeginEditable "Body1" -->This is Test Text<!-- #EndEditable --><!-- #BeginEditable "Extra" -->This is more test text<!-- #EndEditable -->
Как вы можете догадаться, мне нужно объединить два, то есть заменить
<!-- #Editable "Body1" -->
с:
This is Test Text
Я начал код здесь. Но у меня проблемы с использованием функции Regex Replace, которая должна быть расположена в самом низу этой функции For / Each ....
//Html Template
string html = "<html><head></head><body><!-- #Editable \"Body1\" --><p>etc etc</p><!-- #Editable \"Extra\" --></body></html>";
//Regions that need to be put in the Html Template
string regions = "<!-- #BeginEditable \"Body1\" -->This is Test Text<!-- #EndEditable --><!-- #BeginEditable \"Extra\" -->This is more test text<!-- #EndEditable -->";
//Create a Regex to only extract what's between the 'Body' tag
Regex oRegex = new Regex("<body.*?>(.*?)</body>", RegexOptions.Multiline);
//Get only the 'Body' of the html template
string body = oRegex.Match(html).Groups[1].Value.ToString();
// Regex to find sections inside the 'Body' that need replacing with what's in the string 'regions'
Regex oRegex1 = new Regex("<!-- #Editable \"(.*?)\"[^>]*>",RegexOptions.Multiline);
MatchCollection matches = oRegex1.Matches(body);
// Locate section titles i.e. Body1, Extra
foreach (Match match in matches)
{
string title = oRegex1.Match(match.ToString()).Groups[1].ToString();
Regex oRegex2 = new Regex("<!-- #BeginEditable \"" + title + "\"[^>]*>(.*?)<!-- #EndEditable [^>]*>", RegexOptions.Multiline);
//
//
// Replace the 'Body' sections with whats in the 'regions' string cross referencing the titles i.e. Body1, Extra
//
//
//
}