我的工作输出看起来像这样...
Node number 1 (172.xxx.123.210):
ERROR: Cannot download Running config: Connection Refused by 172.xxx.123.210
Node number 2 (172.xxx.124.162):
ERROR: Cannot download Running config: Connection Refused by 172.xxx.124.162
我想处理失败的括号中的那些IP地址。我已经拼凑了一个VB函数,其中包含一个正则表达式来提取IP,它的运行效果很好。不过要注意的^.*\((\d.*)\)
是,正则表达式正在括号内寻找一系列字符,例如...
Node number 1 (172.123.123.210):
当我在不包含方括号的行上调用该函数时,它只会返回零。我没有信誉点可允许我发布图像,因此如果您喜欢查看输出,可以快速尝试!
如果括号中没有IP,我不想返回零,我希望它只是将单元格留空。关于如何调整功能来执行此操作的任何建议?
这是我正在使用的功能...
Function getIP(info As String)
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
RE.Pattern = "^.*\((\d.*)\)"
RE.Global = True
RE.IgnoreCase = True
Set allMatches = RE.Execute(info)
If (allMatches.Count <> 0) Then
result = allMatches.Item(0).SubMatches.Item(0)
End If
getIP = result
End Function
我发现使用两种正则表达式模式可以识别带括号或不带括号的IP地址:
Private Sub CommandButton1_Click()
Set regEx = CreateObject("vbscript.regexp")
Set regEx2 = CreateObject("vbscript.regexp")
regEx.Global = True
regEx.IgnoreCase = True
regEx.Pattern = "\(([0-9]{3}).([0-9]{3}).([0-9]{3}).([0-9]{3})\)"
regEx2.Global = True
regEx2.IgnoreCase = True
regEx2.Pattern = "([0-9]{3}).([0-9]{3}).([0-9]{3}).([0-9]{3})"
For i = 1 To UsedRange.Rows.Count
Set allMatches = regEx.Execute(Cells(i, 1).Value)
Set otherMatches = regEx2.Execute(Cells(i, 1).Value)
If allMatches.Count <> 0 Then
Cells(i, 1).Interior.ColorIndex = 33
End If
If otherMatches.Count <> 0 Then
Cells(i, 1).Interior.ColorIndex = 33
End If
Next i
End Sub
这将遍历第1列,在该列中我写了一些IP地址,并突出显示所有有效IP地址(带或不带括号),并将所有非IP地址留空。
我发现在将两个版本的正则表达式都包含OR时不起作用,并且我不确定为什么,也许与定括号有关吗?
希望您觉得这个有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句