用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - vb代码库

正则套路

2020-08-23 作者: 麒麟举报

[vb]代码库

Function RegMatch(ByVal S, pString) '返回正则匹配的集合
    Dim regex As Object, matchs
    Dim Temp, n
    Set regex = CreateObject("VBScript.RegExp")
    With regex
        .Global = True
        .IgnoreCase = True
        .Pattern = pString
        Set matchs = .Execute(S)
    End With
    Set regex = Nothing
    Set RegMatch = matchs
    Set matchs = Nothing
End Function
Function RegReplace(S, pstrt, rstr) '正则替换
    Dim regex As Object
    Dim Temp
    Set regex = CreateObject("VBScript.RegExp")
    With regex
        .Global = True
        .Pattern = pstrt
        Temp = .Replace(S, rstr)
    End With
    RegReplace = Temp
    Set regex = Nothing
End Function
Public Function RegGet(S, pString, Optional m = 0, Optional sp = "")
    '返回正则匹配\d[^_]*\d
    'm-是否多个
    'sp-分隔符
    Dim matchs, regex
    Dim ss, n
    Set regex = CreateObject("VBScript.RegExp")
    With regex
        .Global = True
        .IgnoreCase = True
        .Pattern = pString
        Set matchs = .Execute(S)
    End With
    If matchs.Count = 0 Then
        RegGet = ""
    Else
        If m = 0 Then '取第一个值
            RegGet = matchs(0).Value
        Else '取多个值
            For Each e In matchs
                ss = ss & sp & e.Value
            Next
            RegGet = Mid(ss, Len(sp) + 1)
        End If
    End If
    Set regex = Nothing
    Set matchs = Nothing
End Function
Public Function RegTest(S, pString) '返回正则匹配\d[^_]*\d
    Dim matchs, regex
    Dim Temp, n
    Set regex = CreateObject("VBScript.RegExp")
    With regex
        .Global = True
        .IgnoreCase = True
        .Pattern = pString
        RegTest = .test(S)
    End With
    Set regex = Nothing
    Set matchs = Nothing
End Function
Public Function RegSum(Rng As Range, Optional m = 0)   '提取数字求和
    Dim matchs, regex
    Set regex = CreateObject("VBScript.RegExp")
    With regex
        .Global = True
        If m <> 0 Then
            .Pattern = "(\-|\+)?\d+(\.\d+)?"   '//带小数点
        Else
            .Pattern = "\d+"    '//不带小数点
        End If
        For Each e In Rng
            Set matchs = .Execute(e.Value)
            For Each Match In matchs
                RegSum = RegSum + Val(Match)
            Next
        Next
    End With
    Set regex = Nothing
    Set matchs = Nothing
End Function



网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...