簡體   English   中英

正則表達式如何匹配值可以為xml類型的屬性

[英]Regular Expression how to match properties where the value can be of type xml

我正在嘗試修復一個無法正常工作的正則表達式。

現在的樣子: <[^>]*(>|$)

字符串以下時,該正則表達式將起作用:

<?UMBRACO_MACRO  macroalias="RelatedLinks"  PushCollection="Test123"  />

但是我需要它來匹配這個:

<?UMBRACO_MACRO  macroalias="RelatedLinksPresentation"  PushCollection="<links><link title="test" link="1058" type="internal" newwindow="1" /></links>"  />

其中一個屬性的內容在xml中。

xml值會產生3組...

編輯:

代碼如下:

protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);


        ...........            

        Regex tagregex = new Regex("<[^>]*(>|$)", RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.Compiled);
        MatchCollection tags = tagregex.Matches(_data.Value.ToString());

        List<int> editornumbers = new List<int>();
        string sortorder = string.Empty;


        for (int i = 0; i < _maxNumber; i++)
        {
            if (!editornumbers.Contains(i))
            {
                string data = string.Empty;

                if (tags.Count > i)
                    data = tags[i].Value;

                MacroEditor macroEditor = new MacroEditor(data, _allowedMacros);
                macroEditor.ID = ID + "macroeditor_" + i;

                this.ContentTemplateContainer.Controls.Add(macroEditor);
            }
        }

        this.ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

        if (tags.Count == _maxNumber)
        {
            _addMacro.Enabled = false;
            _limit.Visible = true;
        }

        MacroContainerEvent.Execute += new MacroContainerEvent.ExecuteHandler(MacroContainerEvent_Execute);

    }

我知道他們為什么這樣做。 他們需要解析它,因為值來自數據庫,並且它可能有多個這樣的字符串,例如

<?UMBRACO_MACRO  macroalias="RelatedLinks"  PushCollection="Test123"  />
<?UMBRACO_MACRO  macroalias="RelatedLinks"  PushCollection="<links><link title="test" link="1058" type="internal" newwindow="1" /></links>"  />
<?UMBRACO_MACRO  macroalias="RelatedLinks"  PushCollection="Test123"  />
<?UMBRACO_MACRO  macroalias="RelatedLinks"  PushCollection="Test123"  />

這是4個宏,並使用正則表達式將其拆分為單獨的實例。

需要明確的是,這不是xml,而是Umbraco框架從其屬性中提取值的一種模式。 這次恰好是值和正則表達式中的xml,它們需要分隔掉UMBRACO_MACRO字符串。

編輯:現在的問題是如何忽略“”之間的所有內容-我什至不希望正則表達式關心值字段中的內容,這可能嗎?

我覺得這很難做,但是我發現了另一條路要走:DI現在Html在保存值之前對值進行編碼,然后當我得到它時,我對其進行解碼,然后就可以了。

要擴展我的評論和Ωmega的評論:

您的數據結構顯然無效,因此無法執行此任務。 您說要忽略引號之間的文本。 原則上講這很好(使用正則表達式也很可能),但是在您的情況下,帶引號的文本包含未轉義的引號。 這導致了這樣的問題,即嵌入式字符串中實際引用的任何內容都將雙引號之外 為了說明這一點,這是將您的字符串分為未加引號和加引號的部分:

<?UMBRACO_MACRO  macroalias=
"RelatedLinks"
PushCollection=
"<links><link title="
test
" link="
1058
" type="
internal
" newwindow="
1
" /></links>"
/>

您的源數據應至少轉義內部引號,如下所示:

<?UMBRACO_MACRO  macroalias="RelatedLinks"  PushCollection="<links><link title=\"test\" link=\"1058\" type=\"internal\" newwindow=\"1\" /></links>"  />

只要您不解決該問題,就將無法解析。

為了能夠使用XML解析器(否則該解析器非常適合此任務),您需要&quot; 字符實體而不是\\"

因此,如果您這樣正確地轉義了源字符串(為清楚起見添加了換行符):

<?UMBRACO_MACRO  macroalias="RelatedLinks"  
  PushCollection="&lt;links&gt;
  &lt;link title=&quot;test&quot; 
  link=&quot;1058&quot; 
  type=&quot;internal&quot; 
  newwindow=&quot;1&quot;
  /&gt;
  &lt;/links&gt;"  
/>

那么您可以使用原始正則表達式。

您可以嘗試將其插入http://txt2re.com/index-csharp.php3 ,然后從此處獲取

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM