[已解决]求一个正则表达式
本帖最后由 hnfeng 于 2016-5-9 11:36 编辑文件中有很多 <ProductKey>........</ProductKey> 这样的文本段,想根据里面的 key 来删掉相应的文本段,但是我使用的表达式会把 第一个 <ProductKey> 到满足条件的 </ProductKey> 之间多个段全删掉。
下面的代码,想要的结果是删掉含有 $a_KeyS 的 文本段(第二段),保留第一段。先谢谢了
local $t_CilxFile="xxxxx " & @CRLF & _
" <ProductKey>" & @CRLF & _
" <KeyDescription>Office15_AccessVL_MAK-Alpha</KeyDescription>" & @CRLF & _
" <KeyId>12345-02162-202-843362-00-2052-7601.0000-0752015</KeyId>" & @CRLF & _
" <KeyType>Mak</KeyType>" & @CRLF & _
" <KeyValue>N3H26-DPD66-4QFF7-DYB3D-KKVT3</KeyValue>" & @CRLF & _
" <LastUpdate xsi:nil=""true"" />" & @CRLF & _
" <LastErrorCode>0</LastErrorCode>" & @CRLF & _
" <RemainingActivations>-1</RemainingActivations>" & @CRLF & _
" <SupportedEditions>ProPlusRetail</SupportedEditions>" & @CRLF & _
" <SupportedSku>064383fa-1538-491c-859b-0ecab169a0ab</SupportedSku>" & @CRLF & _
" <UserRemarks />" & @CRLF & _
" <KeyTypeName>MAK</KeyTypeName>" & @CRLF & _
" </ProductKey>" & @CRLF & _
" <ProductKey>" & @CRLF & _
" <KeyDescription>Office16_AccessVL_MAK</KeyDescription>" & @CRLF & _
" <KeyId>12345-02162-201-810144-00-2052-7601.0000-0752015</KeyId>" & @CRLF & _
" <KeyType>Mak</KeyType>" & @CRLF & _
" <KeyValue>N4CPB-2W4QY-HTRPD-GC6GM-KD43D</KeyValue>" & @CRLF & _
" <LastUpdate xsi:nil=""true"" />" & @CRLF & _
" <LastErrorCode>0</LastErrorCode>" & @CRLF & _
" <RemainingActivations>-1</RemainingActivations>" & @CRLF & _
" <SupportedEditions>ProPlusRetail</SupportedEditions>" & @CRLF & _
" <SupportedSku>064383fa-1538-491c-859b-0ecab169a0ab</SupportedSku>" & @CRLF & _
" <UserRemarks />" & @CRLF & _
" <KeyTypeName>MAK</KeyTypeName>" & @CRLF & _
" </ProductKey> xxxxxxxxxx"
MsgBox(0,0,$t_CilxFile)
Local $a_KeyS="N4CPB-2W4QY-HTRPD-GC6GM-KD43D"
$t_CilxFile=StringRegExpReplace($t_CilxFile, "(?s)<ProductKey>.*?" & $a_KeyS & ".*?</ProductKey>","")
MsgBox(0,0,$t_CilxFile)
楼主是这个意思么?这句正则也是之前别人帮我写的,我照着拆开看能看懂,不照着看自己写又写不出来,脑瓜不好使了。
local $t_CilxFile="xxxxx " & @CRLF & _
" <ProductKey>" & @CRLF & _
" <KeyDescription>Office15_AccessVL_MAK-Alpha</KeyDescription>" & @CRLF & _
" <KeyId>12345-02162-202-843362-00-2052-7601.0000-0752015</KeyId>" & @CRLF & _
" <KeyType>Mak</KeyType>" & @CRLF & _
" <KeyValue>N3H26-DPD66-4QFF7-DYB3D-KKVT3</KeyValue>" & @CRLF & _
" <LastUpdate xsi:nil=""true"" />" & @CRLF & _
" <LastErrorCode>0</LastErrorCode>" & @CRLF & _
" <RemainingActivations>-1</RemainingActivations>" & @CRLF & _
" <SupportedEditions>ProPlusRetail</SupportedEditions>" & @CRLF & _
" <SupportedSku>064383fa-1538-491c-859b-0ecab169a0ab</SupportedSku>" & @CRLF & _
" <UserRemarks />" & @CRLF & _
" <KeyTypeName>MAK</KeyTypeName>" & @CRLF & _
" </ProductKey>" & @CRLF & _
" <ProductKey>" & @CRLF & _
" <KeyDescription>Office16_AccessVL_MAK</KeyDescription>" & @CRLF & _
" <KeyId>12345-02162-201-810144-00-2052-7601.0000-0752015</KeyId>" & @CRLF & _
" <KeyType>Mak</KeyType>" & @CRLF & _
" <KeyValue>N4CPB-2W4QY-HTRPD-GC6GM-KD43D</KeyValue>" & @CRLF & _
" <LastUpdate xsi:nil=""true"" />" & @CRLF & _
" <LastErrorCode>0</LastErrorCode>" & @CRLF & _
" <RemainingActivations>-1</RemainingActivations>" & @CRLF & _
" <SupportedEditions>ProPlusRetail</SupportedEditions>" & @CRLF & _
" <SupportedSku>064383fa-1538-491c-859b-0ecab169a0ab</SupportedSku>" & @CRLF & _
" <UserRemarks />" & @CRLF & _
" <KeyTypeName>MAK</KeyTypeName>" & @CRLF & _
" </ProductKey> xxxxxxxxxx"
MsgBox(0,0,$t_CilxFile)
Local $a_KeyS="N4CPB-2W4QY-HTRPD-GC6GM-KD43D"
$t_CilxFile=StringRegExpReplace($t_CilxFile, "(?s)<ProductKey>(?:(?!ProductKey>).)*?" & $a_KeyS & ".*?</ProductKey>","")
MsgBox(0,0,$t_CilxFile)
回复 2# zch11230
非常感谢!
经初步测试没问题,就是这个意思。
没看懂,为何是 (?!ProductKey>) ,我换为 (?!<ProductKey>) 或 (?!</ProductKey>) 好像也可以。
(?s)<ProductKey>(?:(?!ProductKey>).)*?" & $a_KeyS & ".*?</ProductKey>
(?s)<ProductKey>(?:(?!<ProductKey>).)*?" & $a_KeyS & ".*?</ProductKey>
(?s)<ProductKey>(?:(?!</ProductKey>).)*?" & $a_KeyS & ".*?</ProductKey>
页:
[1]