使用正则表达式提取并打印所需的值,其余部分无需修改

时间:2019-02-22 16:42:17

标签: regex excel vba

我有一个数组“ arr”,内容如代码中所示

Sub z()
Dim regex As Object, allMatches As Object, match As Object
Dim arr(1 To 16)
Dim str As String

arr(1) = "{ value:{90914793497} }"
arr(2) = "{ iPBAdd:{iPBV4Add:{192.168.1.15}} }"
arr(3) = "859272608"
arr(4) = "pocbh"
arr(5) = "0x00 01"
arr(6) = "{ iPAdd:{iPBAdd:{iPBV4Add:{192.168.33.1}}} }"
arr(7) = "TRUE"
arr(8) = "{ :{qRd:{-} dVGU:{1280} dVGD:{4224} cC:{rC} cT:{2018-09-21 14:05:30 -03:00 } uLI:{-} eInf:{qCI:{9} mRB:{-} rth:{5} aMBL:{-}}} }"
arr(9) = "2/21/2019 14:04"
arr(10) = "39"
arr(11) = "normalR"
arr(12) = "{ mSCause:{1} }"
arr(13) = "{ value:{677607185} }"
arr(14) = "GMT"
arr(15) = "-"
arr(16) = "{ :{GHH} }"

Set regex = CreateObject("vbscript.regexp")
With regex
    .Global = True
    .MultiLine = False
    .Pattern = "\b[\d\.\-: ]+\b"
End With

For i = 1 To 16
    Set allMatches = regex.Execute(arr(i))
    For Each match In allMatches
        If i = 8 Then
            str = str & "|" & match.Value
        Else
            str = match.Value
        End If
    Next
    If i = 8 Then
        Debug.Print Trim(Mid(str, 2, Application.Search(" -", str) - 2))
    Else
        Debug.Print Trim(str)
    End If
    str = ""
Next
End Sub

我想提取{}内的所有值。通常,数组的每个项目在{}之间只有一个值,但对于项目arr(8) {}中有多个值,对于该项目,我只希望dVGU,dVGD和date / hout之后的值不带-03:00

我的代码仍然工作正常,正在提取所需的值,但是我也想打印不匹配的值。

我当前的输出是:

90914793497
192.168.1.15
859272608

01
192.168.33.1

1280|4224|2018-09-21 14:05:30
2019 14:04
39

1
677607185

我想要这样的输出

90914793497                                          
192.168.1.15                 
859272608                    
pocbh                        
0x00 01                      
92.168.33.1                  
TRUE                         
1280|4224|2018-09-21 14:05:30
2/21/2019 14:04              
39                           
normalR                      
1                            
677607185                    
GMT                          
-                            
GHH     

因此,输出pocbh, 0x00 01, TRUE, normalR, GMT, -, GHH中缺少

我该如何解决?

1 个答案:

答案 0 :(得分:2)

您可以简单地首先检查字符串中是否有myCompletable.subscribeOn(SubscribeScheduler) .observeOn(ObserveScheduler) .subscribe(this::onComplete, this::onError); 。如果不是,请将{设置为数组值。

str

输出:

...
For i = 1 To 16
    If InStr(1, arr(i), "{") = 0 Then 
        str = arr(i)
    Else
        Set allMatches = regex.Execute(arr(i))
        For Each match In allMatches
            If i = 8 Then
                str = str & "|" & match.Value
            Else
                str = match.Value
            End If
        Next
        If allMatches.Count = 0 Then
            str = alternate_pattern(CStr(arr(i)))
        End If
    End If
    If i = 8 Then
        Debug.Print Trim(Mid(str, 2, Application.Search(" -", str) - 2))
    Else
        Debug.Print Trim(str)
    End If
    str = ""
Next
End Sub

Function alternate_pattern(str As String) As String
Dim regex As Object, matches As Object
Set regex = CreateObject("vbscript.regexp")
With regex
    .Global = True
    .MultiLine = False
    .Pattern = "\b[\w\.\-: ]+\b"
End With

Set matches = regex.Execute(str)
If matches.Count > 0 Then
    alternate_pattern = matches(0)
Else
    alternate_pattern = str
End If

End Function

请注意,最可能有一种更好的方法来说明末尾90914793497 192.168.1.15 859272608 pocbh 0x00 01 192.168.33.1 TRUE 1280|4224|2018-09-21 14:05:30 2/21/2019 14:04 39 normalR 1 677607185 GMT - GHH 。您当前的正则表达式模式仅在寻找数字(使用GHH),在某些情况下,\d中包含字母。这就是{}检查的内容。

我对Regex不太满意,但是我认为您可以在其中进行alternate_pattern声明,或者将它们分组? ...我不知道。重点是,可以肯定地改善这一点,但似乎可以对您拥有的示例进行工作。