【问题标题】:VBA: Regex and Dir() function questionsVBA:Regex 和 Dir() 函数问题
【发布时间】:2012-05-12 20:21:02
【问题描述】:

我使用 VBA 编写了一个简单的脚本。 (我需要用excel优化一些工作)。

关于正则表达式的第一个问题:

正如我之前所说,我使用了 VBA。

简单任务:获取模式匹配并捕获子匹配。

我的代码是:

Dim ResStr as Object
Dim LastStr as Object
Dim RE as Object


Set RE = CreateObject("vbscript.regexp") 'create a regex
With RE
    .MultiLine = False  'm-key
    .Global = False     'g-key
    .IgnoreCase = False 'i-key
    .Pattern = "[<]TD\s+class=gm[>](\d+\.\d+)[<][/]TD[>]" 'tag
End With

Set ResStr = RE.Execute(StrDollar)  'use regex  
Set LastStr = ResStr(0).SubMatches  'get submatch

如何获得 LAST 匹配和 LAST 子匹配? (长度属性?)

关于Dir函数的第二个问题:

如何过滤文件?

我在msdn上看到了这段代码:

   ' Display the names in C:\ that represent directories.
   MyPath = "c:\"   ' Set the path.
   MyName = Dir(MyPath, vbDirectory)   ' Retrieve the first entry.
   Do While MyName <> ""   ' Start the loop.
     ' Use bitwise comparison to make sure MyName is a directory.
     If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
       ' Display entry only if it's a directory.
       Debug.WriteLine(MyName)
     End If   
     MyName = Dir()   ' Get next entry.
  Loop

If (GetAttr(MyPath &amp; MyName) And vbDirectory) = vbDirectory Then - 停止'msdn-guy'!你在开玩笑吗?它只有一种方法吗?

有什么方法可以制作普通的滤镜,而不是这种巨大的线法?

【问题讨论】:

  • (1) 您是否有一个示例字符串供我们测试,以便为您提供最后一场比赛? (2) 你有两个非常不同的问题,应该这样问
  • 20.20 22.22

标签: regex vba excel directory


【解决方案1】:

要获取所有匹配项、子匹配项、长度等,您可以使用类似这样的内容 - 我添加了一个工作示例,该示例使用更简单的模式进行演示(即,将数字序列与非数字匹配)

您应该在假定已找到匹配项之前Test您的正则表达式以避免错误

Sub Test()
Dim RE As Object
Dim strSample As String
Dim ResStr As Object
Dim LastStr As Object
strSample = "123dd6789a"
Set RE = CreateObject("vbscript.regexp")    'create a regex
With RE
    .MultiLine = False  'm-key
    .Global = True     'g-key
    .IgnoreCase = False    'i-key
    .Pattern = "\d+([^\d])"
End With
If RE.Test(strSample) Then
    Set ResStr = RE.Execute(strSample)
    For Each LastStr In ResStr
        MsgBox "Match: " & LastStr & vbNewLine & "SubMatch: " & LastStr.submatches(0) & vbNewLine & "Position: " & LastStr.firstindex + 1 & vbNewLine & "Length: " & LastStr.Length
    Next
End If
End Sub

【讨论】:

    【解决方案2】:

    问题 1

    你可以试试这个:

    Sub Test(StrDollar)
    Dim LastStr As Object
    Dim RE As New RegExp
    Dim mt As Match
    
    
    With RE
        .MultiLine = False  'm-key
        .Global = True     'g-key
        .IgnoreCase = False 'i-key
        .Pattern = "<TD\s+class=gm>(\d+\.\d+)</TD>" 'tag
        For Each mt In .Execute(StrDollar)
             Set LastStr = mt.SubMatches  'get submatch
        Next mt
    End With
    
    MsgBox LastStr(0)
    
    End Sub
    

    问题2

    也许dir() 函数无法根据扩展名创建文件列表。

    [编辑]

    Sub Test2(StrDollar)
    Dim LastStr As Object
    Dim RE As New RegExp
    Dim mt As Match
    Dim dic As New Scripting.Dictionary 'scripting runtime
    Dim pos&
    
    With RE
        .MultiLine = False  'm-key
        .Global = True     'g-key
        .IgnoreCase = False 'i-key
        .Pattern = "<TD\s+class=gm>(\d+\.\d+)</TD>" 'tag
        For Each mt In .Execute(StrDollar)
            pos = pos + 1
            dic.Add CStr(pos), mt.FirstIndex & "||" & mt.Value & "||" & mt.SubMatches(0)
        Next mt
    End With
    
    MsgBox dic.item(CStr(pos))
    
    End Sub
    

    [/编辑]

    【讨论】:

    • 太棒了!但是最后一场比赛怎么样,如果我有的话?像这样:.Pattern="&lt;TD\s+class=gm&gt;(\d+)\.(\d+)&lt;/TD&gt;"。以及如何在匹配和子匹配中建立索引?
    猜你喜欢
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    相关资源
    最近更新 更多