函数参考


StringRegExpReplace

基于正则表达式的文本替换.

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

参数

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

返回值

Success: Returns an updated string based on regular expressions.
Failure: Set @error.
@Error: 描述
0 完全执行. 检查 @Extended 来得到被替换的数量.
2 Pattern 无效. @Extended = pattern 中错误的偏移量.

注意/说明

To separate back-references from actual (replaced) numbers, wrap it with half-round brackets, i.e: "${1}5".
If "\" need to be in the replaced string they must be doubled. This is a consequence of the back-references mechanism.

相关

StringRegExp

示例/演示


Test1()
Test2()
Test3()

; This example demonstrates a basic replacement.  It replaces the vowels aeiou
; with the @ character.
Func Test1()
    Local $sInput = "Where have all the flowers gone, long time passing?"
    Local $sOutput = StringRegExpReplace($sInput, "[aeiou]", "@")
    Display($sInput, $sOutput)
EndFunc   ;==>Test1

; The following example demonstrates using back-references to change the date
; from MM/DD/YYYY to 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

; The following example demonstrates the need to double backslash
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)
    ; Format the output.
    Local $sMsg = StringFormat("Input:\t%s\n\nOutput:\t%s", $sInput, $sOutput)
    MsgBox(0, "Results", $sMsg)
EndFunc   ;==>Display