基 础 函 数 参 考


StringRegExpReplace

正则表达式字符替换.

StringRegExpReplace ( "字符串", "表达式", "替换", [ 数量 ] )

参 数

test 目标字符串
表达式 正则表达式比较. 参考 StringRegExp 的详细讲解.
替换 替换匹配正则表达式匹配值的文本. 插入匹配组文本, \0 - \9 (或 $0 - $9) 可以作为一个逆向引用.
数量 [可选参数] 需要执行替换的次数. 默认 0, 全局替换.

返 回 值

成功: 返回基于正则表达式更新后的字符串.
失败: 设置 @error.
返回 @Error: 意义
0 正确执行. @Extended 包含替换的数量.
2 "表达式"无效. @Extended = "表达式"的错误偏移量.

备 注

从实际(替换)结果分别逆向引用, 以大括号包围, 例如: "${1}5".
要替换 "\", 必须使用两个相同字符. 这是一个反向引用机制的后果.

相 关 函 数

StringRegExp

函 数 示 例


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, "结果", $sMsg)
EndFunc   ;==>Display

provider with jb51.net (unicode)