是否可以刪除內部的括號但保留CDATA標記不變?怎麼樣?刪除括號但保留CDATA不變
<![CDATA[
This is some Text with [brackets inside]
]]>
編輯:我用PHP,對不起。
編輯二:我看到我可以使用lookaround斷言,但有點不知道如何做的AND連接器,可能是在CDATA之前或之後,以及如何連接最後兩個塊。
是否可以刪除內部的括號但保留CDATA標記不變?怎麼樣?刪除括號但保留CDATA不變
<![CDATA[
This is some Text with [brackets inside]
]]>
編輯:我用PHP,對不起。
編輯二:我看到我可以使用lookaround斷言,但有點不知道如何做的AND連接器,可能是在CDATA之前或之後,以及如何連接最後兩個塊。
這裏有您需要的正則表達式:
$subject = 'your_input_text';
$matchPattern = '/(<!\[CDATA\[[^[]*)\[(.*?)\]([^\]]*\]\]>)/s';
$replacePattern = '$1$2$3';
$result = preg_replace($matchPattern, $replacePattern, $subject);
你可以看到的結果here。
而這裏的正則表達式的解釋:
# (<!\[CDATA\[[^\[]*)\[(.*?)\]([^\]]*\]\]>)
#
# Options: dot matches newline
#
# Match the regular expression below and capture its match into backreference number 1 «(<!\[CDATA\[[^\[]*)»
# Match the characters 「<!」 literally «<!»
# Match the character 「[」 literally «\[»
# Match the characters 「CDATA」 literally «CDATA»
# Match the character 「[」 literally «\[»
# Match any character that is NOT a [ character «[^\[]*»
# Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
# Match the character 「[」 literally «\[»
# Match the regular expression below and capture its match into backreference number 2 «(.*?)»
# Match any single character «.*?»
# Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
# Match the character 「]」 literally «\]»
# Match the regular expression below and capture its match into backreference number 3 «([^\]]*\]\]>)»
# Match any character that is NOT a ] character «[^\]]*»
# Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
# Match the character 「]」 literally «\]»
# Match the character 「]」 literally «\]»
# Match the character 「>」 literally «>»
// uses MFC CString syntax but any string library will have the same essential functions
CString myCDATA;
CString workString;
int iTagStart;
iTagStart = String.Find("<![CDATA[") + 9 // 9 = length of "<![CDATA["
// get the string without the endpoints
workString = CString.Mid(iTagStart,myCDATA.GetLength() - 9 - 3); // 3 = length of "]]>"
workString.Replace("[","");
workString.Replace("]","");
// reassemble
myCDATA = "<![CDATA[" + workString + "]]>";
這是一個好主意,但我的文檔中有多個CDATA部分,因此這不起作用。 – shredding
您使用什麼語言? – Eric
呃,如果你使用PHP,那你爲什麼不想使用lookaround斷言?這*完全是*他們是爲... –
它在PHP中看起來如何?我不知道它在PHP 5.2中。 – shredding