Skip to content

Instantly share code, notes, and snippets.

@tst32
Last active June 25, 2018 15:02
Show Gist options
  • Save tst32/e9283d8f9bee6e25eee0cc25149364da to your computer and use it in GitHub Desktop.
Save tst32/e9283d8f9bee6e25eee0cc25149364da to your computer and use it in GitHub Desktop.
vb with regex
XXXA XXXB XXXC
172.28.16.0/23 1
10.57.0.0/26 1
192.168.1.0/24 1
Sub NewRegex()
'
Dim para As Paragraph
Dim text As Variant
Dim wordtext As Variant
Dim sPath As String
Dim sFile As String
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim RegexPattern As String
Dim re As New RegExp
Dim Matches As Variant
Dim I, x, cnt As Long
cnt = Range("a2", Range("a2").End(xlDown)).Rows.Count 'СТОЛБЕЦ C ПОДСЕТКАМИ
'TODO цикл по строке 2
Set oWord = CreateObject("Word.Application")
sPath = CreateObject("WScript.Shell") _
.specialfolders("Desktop") & "\" & "test"
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
sFile = Dir(sPath & "*" & "XXXA" & "*.docx")
'https://regex101.com/r/VHSSbJ/2
RegexPattern = "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.)){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/(3[0-2]|(1|2)?[0-9]))?"
Set oDoc = oWord.Documents.Open(sPath & sFile)
With re
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = RegexPattern
End With
For Each para In oDoc.Paragraphs
wordtext = wordtext & para.Range.text
Next para
Set Matches = re.Execute(wordtext)
Debug.Print Matches.Count
For x = 2 To cnt + 1 'ОСНОВНОЙ ЦИКЛ ПО СТОЛБЦУ
text = Cells(x, 1).Value
For I = 0 To Matches.Count - 1
If text Like Matches(I) Then
Debug.Print ("YES+ "), text
Cells(x, 2).Value = "1" 'ЕСТЬ ПОДСЕТЬ СТАВИМ 1
End If
Next I
Next x
End Sub
ID XXXA
Назначение сети Октеты
tinao.mskobr.ru 172.28.16.0/23
asus.router 192.168.1.0/24
mgts.ru 10.57.0.0/26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment