20130918

VBScript 尋找符合模式之字串

WScript.echo PATINDEX("abc", "dddabcdddabc")

Function PATINDEX(pat, strin)
    Set RegEx = CreateObject("VBscript.regexp")
    RegEx.MultiLine = True
    RegEx.pattern = pat

    set matchs = RegEx.Execute(strin)
    if matchs.count = 0 then
        PATINDEX = -1
    else
        PATINDEX = matchs(0).firstindex
    end if
    Set RegEx = Nothing
End Function

Execute 方法

輸入字串後,並傳回符合模式之 Match 集合 Matchs,若輸入字串沒有子字串符合, Matchs.Count 會等於零。

若 RegExp.Global 屬性為 False(預設值),則MatchCollection 只包含第一個符合的子字串,
否則包含所有符合的子字串。

Match 物件

用來表示成功的比對結果,其屬性是唯讀的。
FirstIndex 表示符合子字串起始位置(位置由0開始);
Length表示符合子字串長度;
Value傳回符合子字串長度。

How to Use the VBScript RegExp Object

You can use regular expressions in VBScript by creating one or more instances of the RegExp object. This object allows you to find regular expression matches in strings, and replace regex matches in strings with other strings. The functionality offered by VBScript's RegExp object is pretty much bare bones. However, it's more than enough for simple input validation and output formatting tasks typically done in VBScript.

The advantage of the RegExp object's bare-bones nature is that it's very easy to use. Create one, put in a regex, and let it match or replace. Only four properties and three methods are available.

After creating the object, assign the regular expression you want to search for to the Pattern property. If you want to use a literal regular expression rather than a user-supplied one, simply put the regular expression in a double-quoted string. By default, the regular expression is case sensitive. Set the IgnoreCase property to True to make it case insensitive. The caret and dollar only match at the very start and very end of the subject string by default. If your subject string consists of multiple lines separated by line breaks, you can make the caret and dollarmatch at the start and the end of those lines by setting the Multiline property to True. VBScript does not have an option to make the dot match line break characters. Finally, if you want the RegExp object to return or replace all matches instead of just the first one, set the Global property to True.'Prepare a regular expression object Set myRegExp = New RegExp myRegExp.IgnoreCase = True myRegExp.Global = True myRegExp.Pattern = "regex"


After setting the RegExp object's properties, you can invoke one of the three methods to perform one of three basic tasks. The Test method takes one parameter: a string to test the regular expression on. Test returns True or False, indicating if the regular expression matches (part of) the string. When validating user input, you'll typically want to check if the entire string matches the regular expression. To do so, put a caret at the start of the regex, and a dollar at the end, to anchor the regex at the start and end of the subject string.



The Replace method takes two string parameters. The first parameter is the subject string, while the second parameter is the replacement text. If the RegExp.Global property is False (the default), Replace will return the subject string with the first regex match (if any) substituted with the replacement text. If RegExp.Global is true,Replace will return the subject string with all regex matches replaced.

You can specify an empty string as the replacement text. This will cause the Replace method to return the subject string will all regex matches deleted from it. To re-insert the regex match as part of the replacement, include amp; in the replacement text. E.g. to enclose each regex match in the string between square brackets, specify [amp;] as the replacement text. If the regexp contains capturing parentheses, you can use backreferences in the replacement text. $1 in the replacement text inserts the text matched by the first capturing group, $2 the second, etc. up to $9. To include a literal dollar sign in the replacements, put two consecutive dollar signs in the string you pass to theReplace method.

沒有留言:

張貼留言