zoukankan      html  css  js  c++  java
  • QTP VBScript RegExp对象的运用

     下面来讲讲RegExp对象:

    Vbs提供了针对正则表达式的一个非常实用的类,就是RegExp
    Global属性:代表全局匹配
    IgnoreCase属性:大小写忽略
    Pattern属性:正则表达式
    Execute方法:匹配搜索,返回匹配结果集合
    Replace方法:匹配代替,返回替代匹配结果
    Test方法:测试匹配,返回布尔类型
    

      下面举几个实例:

    '判断正则匹配是否正确
    'msgbox (IsRegMatch("a123","http://www.123.456.com"))
    Function IsRegMatch(patrn,str)
    Dim regEx
    Set regEx = New RegExp
    regEx.Pattern = patrn
    regEx.IgnoreCase = False
    IsRegMatch = regEx.Test(str)
    Set regEx = nothing
    End Function
    '替换匹配字符串
    'msgbox (ReplaceRegMatch("9","loader runner 9.0, qtp 9.0","10"))
    Function ReplaceRegMatch(patrn,str,replaceStr)
    Dim regEx
    Set regEx = New RegExp
    regEx.Pattern = patrn
    regEx.IgnoreCase = False
    regEx.Global = True   'false的时候只会替换第一个匹配的字符串。若为true则会替换所有匹配的字符串
    ReplaceRegMatch = regEx.Replace(str,replaceStr)
    End Function
    '返回匹配内容
    'returnRegMatch "qtp .","qtp 1 qtp 2 qtp3 qtp 4"
    Function ReturnRegMatch(patrn,str)
    Dim regEx,matches,match
    Set regEx = New RegExp
    regEx.Pattern = patrn
    regEx.IgnoreCase = true
    regEx.Global = true  '打开全局搜索
    Set matches = regEx.Execute(str)
    For Each match in matches
    print cstr(match.firstIndex) + " " + match.value + " " + cstr(match.length)
    Next
    End Function
    

    最新内容请见作者的GitHub页:http://qaseven.github.io/

  • 相关阅读:
    codeforce 148D. Bag of mice[概率dp]
    poj2096 Collecting Bugs[期望dp]
    poj3744 Scout YYF I[概率dp+矩阵优化]
    hdu 5318 The Goddess Of The Moon
    hdu5411 CRB and Puzzle[矩阵优化dp]
    poj3734 Blocks[矩阵优化dp or 组合数学]
    1948 NOI 嘉年华
    教主泡嫦娥[有趣的dp状态设计]
    HDU 4455 Substrings[多重dp]
    1296: [SCOI2009]粉刷匠[多重dp]
  • 原文地址:https://www.cnblogs.com/twodog/p/12140156.html
Copyright © 2011-2022 走看看