0

内部のブラケットを削除しても、CDATAタグはそのままにしておくことはできますか?どのように?

<![CDATA[

This is some Text with [brackets inside]

]]>

編集:申し訳ありませんが、PHPを使用しています。

編集II:ルックアラウンドアサーションを使用できるようですが、CDATAの前または後のどちらかに開始ブラケットがある可能性があるANDコネクタの実行方法と、最後の2つのチャンクの接続方法に少し気づいていません。

4

2 に答える 2

2

必要な正規表現は次のとおりです。

$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 “&lt;!” 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 “&gt;” literally «>»
于 2012-09-09T13:15:32.457 に答える
1
// 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 + "]]>";
于 2012-09-09T12:27:35.840 に答える