正則表達(dá)式字符替換.
StringRegExpReplace ( "字符串", "表達(dá)式", "替換", [
數(shù)量 ] )
test | 目標(biāo)字符串 |
表達(dá)式 | 正則表達(dá)式比較. 參考 StringRegExp 的詳細(xì)講解. |
替換 | 替換匹配正則表達(dá)式匹配值的文本. 插入匹配組文本, \0 - \9 (或 $0 - $9) 可以作為一個(gè)逆向引用. |
數(shù)量 | [可選參數(shù)] 需要執(zhí)行替換的次數(shù). 默認(rèn) 0, 全局替換. |
成功: | 返回基于正則表達(dá)式更新后的字符串. |
失敗: | 設(shè)置 @error. |
返回 @Error: | 意義 |
0 | 正確執(zhí)行. @Extended 包含替換的數(shù)量. |
2 | "表達(dá)式"無(wú)效. @Extended = "表達(dá)式"的錯(cuò)誤偏移量. |
Test1()
Test2()
Test3()
; 基本替換演示例子
; 用 “@” 字符替換原字符串中的元音字母.
Func Test1()
Local
$sInput =
"Where have all the flowers gone, long time
passing?"
Local
$sOutput =
StringRegExpReplace($sInput, "[aeiou]", "@")
Display($sInput, $sOutput)
EndFunc ;==>Test1
; 演示如何使用反向引用
; 更改日期格式 MM/DD/YYYY 替換為 DD.MM.YYYY
Func Test2()
Local
$sInput =
'some text1 12/31/2009 01:02:03 some text2'
& @CRLF
& _
'some text3 02/28/2009 11:22:33 some
text4'
Local
$sOutput =
StringRegExpReplace($sInput, '(\d{2})/(\d{2})/(\d{4})',
' $2.$1.$3 ')
Display($sInput, $sOutput)
EndFunc ;==>Test2
; 下面的例子演示使用雙反斜線
Func Test3()
Local
$sInput =
'%CommonProgramFiles%\Microsoft
Shared\'
Local
$sOutput =
StringRegExpReplace($sInput, '%([^%]*?)%', 'C:\\WINDOWS\\Some Other
Folder$')
Display($sInput, $sOutput)
EndFunc ;==>Test3
Func Display($sInput, $sOutput)
;
格式化輸出.
Local
$sMsg =
StringFormat("原字符串:\t%s\n\n替換后:\t%s",
$sInput,
$sOutput)
MsgBox(0, "結(jié)果", $sMsg)
EndFunc ;==>Display
provider with jb51.net (unicode) |