请教正则文本替换
本帖最后由 zhwu530 于 2012-7-14 10:47 编辑脑袋生得笨,自己看帮助头都大了还是没弄明白,只好来论坛请教了,
有个名为 index.php 的文件内容是: <title>正则</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/swfobject.js" type="text/javascript"></script>
<script src="js/rightclick.js" type="text/javascript"></script>
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/jquery_cookie.js" type="text/javascript"></script>
<script src="js/swffit.js" type="text/javascript"></script>
<script type="text/javascript">
function navigateToSignIn(){window.onbeforeunload=null;window.location.href='http://127.0.0.1';}
function navigateToReg(){navigateToSignIn();}
function navigateToLogin(){navigateToSignIn();}
function OnYouKeSession(session_id){}
function OnScreenMode(minw,minh,maxw,maxh){
swffit.fit("customRightClick",minw,minh,maxw,maxh);
}
function refreshThisPage()
{
document.location.href=document.location.href;
}
function game_shouchang(){
window.external.addFavorite('http://127.0.0.1/','正则');
}
function OnSetChinaLimit(realname,cid,sid){
if (realname != ""&&cid!=""){
$.ajax({
type: "GET",
url: "regfcm.php?realname="+realname+"&cid="+cid+"&sid="+sid,
cache: false,
success: function(result){
}
});
}else{
return "fcmerror";
}
return "fcmok";
}$tihuan1='替换'
$tihuan2='192.168.1.1'
想用正则把代码中<title>正则</title>部分和window.external.addFavorite('http://127.0.0.1/','正则');
中的'正则''替换为 $tihuan1,'http://127.0.0.1/部分替换为 $tihuan2 该怎么写,最好加上说明,方便理解,
(比如用到 <(?i)test>(.*?)</(?i)test> 这些,为什么要用到这些) 真的一窍不通,真心就解,望有心人教教。。 共有二处“正则”,都要替换的话,直接用StringReplace()函数吧。
共有一处“window.external.addFavorite('http://127.0.0.1/','正则');”,替换也可直接用StringReplace()函数。 就是不知道怎么写。 正则大神们都哪里去了?来帮帮我吧。。。。 $text_tihuan1 = StringRegExpReplace($Text,'正则',$tihuan1) 谢谢楼上的回答,
<title>我是随机的</title>
$arr = array ('game_ip'=>"117.41.229.50",
window.external.addFavorite('http://117.41.229.50/','寂寞的时候烟来陪');
$url ='http://117.41.229.50:99/app/cklogin.php?
define('GAMEURL','http://117.41.229.50:99/indexreal.html');
define('DBHOST', '127.0.0.1:3306');
$txt1='哇噻正则'
$txt2='192.168.1.10'
(“我是随机的”和“寂寞的时候烟来陪”这2处实际上我们不知道他是什么,有可能是“江泽民”“宋祖英”所以要替换)
()
上面出现中文的地方替换成 $txt1出现IP地址的地方替换成$txt2 $text = "<title>我是随机的</title>" &@CRLF _
& '$arr = array("game_ip"=>"117.41.229.50,' &@CRLF _
& 'window.external.addFavorite(http://117.41.229.50/,"寂寞的时候烟来陪")' & @CRLF _
& '$url ="http://117.41.229.50:99/app/cklogin.php?"'&@CRLF _
& 'define("GAMEURL","http://117.41.229.50:99/indexreal.html");' &@CRLF _
& 'define("DBHOST", "127.0.0.1:3306");'
MsgBox(0,"替换前的文本",$text)
$txt1='哇噻正则'
$txt2='192.168.1.10'
$text = StringRegExpReplace($text,'[\x{4e00}-\x{9fa5}]+',$txt1,3);中文替换
$text = StringRegExpReplace($text,'(\d{1,3}\.){3}\d{1,3}',$txt2,0);IP替换
MsgBox(0,"替换后的文本",$text)
回复 6# zhwu530 Local $Str = _
' <title>正则</title>' & @CRLF & _
' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' & @CRLF & _
' <script src="js/swfobject.js" type="text/javascript"></script>' & @CRLF & _
' <script src="js/rightclick.js" type="text/javascript"></script>' & @CRLF & _
' <script src="js/jquery.js" type="text/javascript"></script>' & @CRLF & _
' <script src="js/jquery_cookie.js" type="text/javascript"></script>' & @CRLF & _
' <script src="js/swffit.js" type="text/javascript"></script>' & @CRLF & _
' <script type="text/javascript">' & @CRLF & _
" function navigateToSignIn(){window.onbeforeunload=null;window.location.href='http://127.0.0.1';}" & @CRLF & _
' function navigateToReg(){navigateToSignIn();}' & @CRLF & _
' function navigateToLogin(){navigateToSignIn();}' & @CRLF & _
' function OnYouKeSession(session_id){}' & @CRLF & _
' function OnScreenMode(minw,minh,maxw,maxh){' & @CRLF & _
' swffit.fit("customRightClick",minw,minh,maxw,maxh);' & @CRLF & _
' }' & @CRLF & _
' function refreshThisPage()' & @CRLF & _
'{' & @CRLF & _
'document.location.href=document.location.href;' & @CRLF & _
'}' & @CRLF & @CRLF & _
' function game_shouchang(){' & @CRLF & _
" window.external.addFavorite('http://127.0.0.1/','正则');" & @CRLF & _
' }' & @CRLF & _
' ' & @CRLF & _
' function OnSetChinaLimit(realname,cid,sid){' & @CRLF & _
' if (realname != ""&&cid!=""){' & @CRLF & _
' $.ajax({' & @CRLF & _
' type: "GET",' & @CRLF & _
' url: "regfcm.php?realname="+realname+"&cid="+cid+"&sid="+sid,' & @CRLF & _
' cache: false,' & @CRLF & _
' success: function(result){' & @CRLF & _
' }' & @CRLF & _
' });' & @CRLF & _
' }else{' & @CRLF & _
' return "fcmerror";' & @CRLF & _
' }' & @CRLF & _
' return "fcmok";' & @CRLF & _
' }'
MsgBox(0, '原字符串', $Str)
Local $tihuan1 = '替换', $tihuan2 = '192.168.1.1'
Local $sRER = StringRegExpReplace($str, '(?i)(?<=<title>)[^<]+', $tihuan1)
$sRER = StringRegExpReplace($sRER, "(?i)(\.addFavorite.+?',')[^']+", '${1}' & $tihuan2)
MsgBox(0, '替换结果', $sRER)
非常感谢!
用7#方法成功,
大神可能是没明白我的意思,
Local $Str = _
' <title>正则</title>' & @CRLF & _
' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' & @CRLF & _
' <script src="js/swfobject.js" type="text/javascript"></script>' & @CRLF & _
' <script src="js/rightclick.js" type="text/javascript"></script>' & @CRLF & _
' <script src="js/jquery.js" type="text/javascript"></script>' & @CRLF & _
' <script src="js/jquery_cookie.js" type="text/javascript"></script>' & @CRLF & _
' <script src="js/swffit.js" type="text/javascript"></script>' & @CRLF & _
' <script type="text/javascript">' & @CRLF & _
" function navigateToSignIn(){window.onbeforeunload=null;window.location.href='http://127.0.0.1';}" & @CRLF & _
' function navigateToReg(){navigateToSignIn();}' & @CRLF & _
' function navigateToLogin(){navigateToSignIn();}' & @CRLF & _
' function OnYouKeSession(session_id){}' & @CRLF & _
' function OnScreenMode(minw,minh,maxw,maxh){' & @CRLF & _
' swffit.fit("customRightClick",minw,minh,maxw,maxh);' & @CRLF & _
' }' & @CRLF & _
' function refreshThisPage()' & @CRLF & _
' define("GAMEURL","http://117.41.229.50:99/indexreal.html");' &@CRLF & _
'{' & @CRLF & _
'document.location.href=document.location.href;' & @CRLF & _
'}' & @CRLF & @CRLF & _
' function game_shouchang(){' & @CRLF & _
" window.external.addFavorite('http://127.0.0.1/','正则');" & @CRLF & _
' }' & @CRLF & _
' ' & @CRLF & _
' function OnSetChinaLimit(realname,cid,sid){' & @CRLF & _
' if (realname != ""&&cid!=""){' & @CRLF & _
' $.ajax({' & @CRLF & _
' type: "GET",' & @CRLF & _
' url: "regfcm.php?realname="+realname+"&cid="+cid+"&sid="+sid,' & @CRLF & _
' cache: false,' & @CRLF & _
' success: function(result){' & @CRLF & _
' }' & @CRLF & _
' });' & @CRLF & _
' }else{' & @CRLF & _
' return "fcmerror";' & @CRLF & _
' }' & @CRLF & _
' return "fcmok";' & @CRLF & _
' }'
MsgBox(0, '原字符串', $Str)
Local $tihuan1 = '替换', $tihuan2 = '192.168.1.1' , $tihuan3= '8080'
Local $sRER = StringRegExpReplace($str, '(?i)(?<=<title>)[^<]+', $tihuan1)
$sRER = StringRegExpReplace($sRER, "(?i)(\.addFavorite.+?',')[^']+", '${1}' & $tihuan2)
MsgBox(0, '替换结果', $sRER)
; 用大神这个没成功,代码中有2处出现了“正则”都要替换成 $tihuan1
; 代码中出现IP的地方都要替换成 $tihuan2
; define("GAMEURL","http://117.41.229.50:99/indexreal.html") 这里出现的“99” 替换成 $tihuan3
; 特别提示: 上述要替换的地方其值不是固定的
;
;
页:
[1]