[已解决] 1.txt文件中字符串用2.txt中相应存在的字符串替换~!!!!
本帖最后由 双双 于 2010-9-13 18:49 编辑1.txt:
<?xml version="1.0" encoding="utf-8"?>
<StringBundle version="1.0">
<String id="AAA">AAA1</String>
<String id="BBB">BBB1</String>
<String id="CCC">CCC1</String>
<!-- PRODUCT_IPC_PLAT-->
......
2.txt:
<?xml version="1.0" encoding="utf-8"?>
<StringBundle version="1.0">
<String id="AAA">甲甲1</String>
<String id="BBB">乙乙1</String>
<String id="CCC">丙丙1</String>
<!-- PRODUCT_IPC_PLAT-->
......
将
1.txt中<String id="后面"~~~双引号中相同的~~的后面的~~~AAA1...BBB1...CCC1~~~换成2.txt中的甲甲1...乙乙1(如果存在)
1.txt中还有别的东东~~~2.txt也还有另外的东东哦~~~
在线等!!!!!!!!!!!!!!! 那你直接要2.txt就好了吗,还替换干啥 1.txt中还有别的东东~~~2.txt也还有另外的东东哦~~~ $Str1 = _
'<?xml version="1.0" encoding="utf-8"?>' & @CRLF & _
'<StringBundle version="1.0">' & @CRLF & _
' <String id="AAA">AAA1</String>' & @CRLF & _
' <String id="BBB">BBB1</String>' & @CRLF & _
' <String id="CCC">CCC1</String>' & @CRLF & _
' <!-- PRODUCT_IPC_PLAT-->'
Msgbox(0, '原字符串', $str1)
$Str2 = _
'<?xml version="1.0" encoding="utf-8"?>' & @CRLF & _
'<StringBundle version="1.0">' & @CRLF & _
' <String id="AAA">甲甲1</String>' & @CRLF & _
' <String id="BBB">乙乙1</String>' & @CRLF & _
' <String id="CCC">丙丙1</String>' & @CRLF & _
' <!-- PRODUCT_IPC_PLAT-->' & @CRLF
Msgbox(0, '原字符串', $str2)
$s2 = StringRegExpReplace($str2, '(?s).+?(<String id.+?<!).+', '$1')
$str1 = StringRegExpReplace($str1, '(?s)<String id.+?<!', $s2)
Msgbox(0, '替换后', $str1)
高明啊,佩服!!!
顺便请教下 替换字符中 $1,什么含义呢? 多谢~~晚上来试~~
回头送你奖品~~~ 本帖最后由 双双 于 2010-9-12 17:37 编辑
$1~~应该是替换字符~~ 回复 5# kxing
反向引用第一个组,知道你已经在另一贴了解了~ $Str1 =_
'<?xml version="1.0" encoding="utf-8"?>' & @CRLF & _
'<StringBundle version="1.0">' & @CRLF & _
' <String id="AAA">AAA1</String>' & @CRLF & _
' <String id="BB">BBB</String>' & @CRLF & _
' <String id="CCC">CCC1</String>' & @CRLF & _
' <!-- PRODUCT_IPC_PLAT-->'
Msgbox(0, '原字符串', $str1)
$Str2 = _
'<?xml version="1.0" encoding="utf-8"?>' & @CRLF & _
'<StringBundle version="1.0">' & @CRLF & _
' <String id="AAA">甲甲1</String>' & @CRLF & _
' <String id="BBB">乙乙1</String>' & @CRLF & _
' <String id="CCC">丙丙1</String>' & @CRLF & _
' <!-- PRODUCT_IPC_PLAT-->' & @CRLF
Msgbox(0, '原字符串', $str2)
$s2 = StringRegExpReplace($str2, '(?s).+?(<String id.+?<!).+', '$1')
$str1 = StringRegExpReplace($str1, '(?s)<String id.+?<!', $s2)
Msgbox(0, '替换后', $str1)有点问题~~`把1.txt"BBB"改为"BB"~~~在2.txt中没有的~~结果还有~~ 1.txt;2.txt~~并不是一一对应的~
1.txt中还有别的东东~~~2.txt也还有另外的东东哦~~~ 回复 10# 双双
问题描述时所举的的例子应当尽量注明多种情况,像一楼的例子都是AAA BBB CCC 肯定不会有预期的答案…
p.s 去掉标题的“有奖励”可能会有更多人回答… 正则偶还不会哦! 本帖最后由 C.L 于 2010-9-12 23:42 编辑
回复 9# 双双
不是一一对应的话:
$txt1 = _
'<?xml version="1.0" encoding="utf-8"?>' & _
'<StringBundle version="1.0">'& _
'<String id="AAA">AAA1</String>'& _
'<String id="BB">BBB1</String>'& _
'<String id="CCC">CCC1</String>'& _
'<!-- PRODUCT_IPC_PLAT-->'
$txt2 = _
'<?xml version="1.0" encoding="utf-8"?>'& _
'<StringBundle version="1.0">'& _
'<String id="AAA">甲甲1</String>'& _
'<String id="BBB">乙乙1</String>'& _
'<String id="CCC">丙丙1</String>'& _
'<!-- PRODUCT_IPC_PLAT-->'
MsgBox (0,'原始文件','1.txt:'&@CRLF&$txt1&@CRLF&'2.txt:'&@CRLF&$txt2)
$aTxt1 = StringRegExp ($txt1,'<String id="(.+?)">(.+?)</String>',3)
$aTxt2 = StringRegExp ($txt2,'<String id="(.+?)">(.+?)</String>',3)
For $i=0 To UBound($aTxt2)-1 Step 2
For $j=0 To UBound ($aTxt1)-1 Step 2
If $aTxt1[$j] == $aTxt2[$i] Then
$txt1 = StringRegExpReplace ($txt1,'<String id="'&$aTxt1[$j]&'">'&$aTxt1[$j+1], _
'<String id="'&$aTxt2[$i]&'">'&$aTxt2[$i+1])
EndIf
Next
Next
MsgBox (0,'$txt1替换后',$txt1) $Str1 = _
'<?xml version="1.0" encoding="utf-8"?>' & @CRLF & _
'<StringBundle version="1.0">' & @CRLF & _
' <String id="AAA">AAA1</String>' & @CRLF & _
' <String id="BBB">BBB1</String>' & @CRLF & _
' <String id="CCC">CCC1</String>' & @CRLF & _
' <String id="xxx">xxx</String>' & @CRLF & _
' <!-- PRODUCT_IPC_PLAT-->'
$Str2 = _
'<?xml version="1.0" encoding="utf-8"?>' & @CRLF & _
'<StringBundle version="1.0">' & @CRLF & _
' <String id="AAA">甲甲1</String>' & @CRLF & _
' <String id="BB">乙乙1</String>' & @CRLF & _
' <String id="CCC">丙丙1</String>' & @CRLF & _
' <!-- PRODUCT_IPC_PLAT-->' & @CRLF
$s2_sR = StringRegExp($str2, '<String id="(.+?)">(.+?)<', 3)
For $i = 0 to UBound($s2_sR) - 1 step 2
$str1 = StringRegExpReplace($str1, '(?<=\<String id="' & $s2_sR[$i] & '">)[^<]+', $s2_sR[$i + 1])
Next
Msgbox(0, '替换后', $str1) 晚上再看~~
三年了~~换了不少人~~可人还是哪好!
页:
[1]