使用正则表达式假设我有 html 作为字符串 如何使用正则表达式从字符串中获取所有小部件控制标记。?
当前方法
const string widgetStartPattern = "<widget:ContentPageView";
const string widgetEndPattern = "/>";
var allOccuranceOfWidgets = CountStringOccurrences(aspx, widgetStartPattern);
while (allOccuranceOfWidgets.Count > 0)
{
var firstIndex = allOccuranceOfWidgets[0];
var lastIndex = aspx.IndexOf(widgetEndPattern, firstIndex + 1, System.StringComparison.OrdinalIgnoreCase);
var widgetUserControlTag = aspx.Substring(firstIndex, lastIndex - firstIndex + 2);
var pageId = ExtractPageIdFromWidgetTag(widgetUserControlTag);
var pageContent = GetContentFromaDatabase(pageId);
aspx = aspx.Replace(widgetUserControlTag, pageContent);
allOccuranceOfWidgets = CountStringOccurrences(aspx, widgetStartPattern);
}
所有小部件控件的结果列表
<widget:ContentPageView id="ContentPageView0" PageId="165" runat="server" />
<widget:ContentPageView id="ContentPageView1" PageId="166" runat="server" />
<widget:ContentPageView id="ContentPageView2" PageId="167" runat="server" />
HTML
<div class="slogan">
<widget:ContentPageView id="ContentPageView0" PageId="165" runat="server" />
</div>
<div class="headertopright">
<div class="headersocial">
<widget:ContentPageView id="ContentPageView1" PageId="166" runat="server" />
</div>
<div class="searchbox">
<widget:ContentPageView id="ContentPageView2" PageId="167" runat="server" />