移除支架,但CDATA保持不变


Removing Brackets but leave CDATA untouched

是否可以去掉里面的括号,但不影响CDATA标记?怎样

<![CDATA[
This is some Text with [brackets inside]
]]>

编辑:对不起,我用的是PHP。

编辑II:我知道我可以使用环视断言,但有点不知道如何进行AND连接,即左括号可能在CDATA之前或之后,以及如何连接最后两个块。

以下是您需要的正则表达式:

$subject = 'your_input_text';
$matchPattern = '/(<!'[CDATA'[[^[]*)'[(.*?)']([^']]*']']>)/s';
$replacePattern = '$1$2$3';
$result = preg_replace($matchPattern, $replacePattern, $subject);

您可以在此处查看结果。

下面是正则表达式模式的解释:

# (<!'[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 + "]]>";