- 帖子
- 1018
- 主題
- 15
- 精華
- 0
- 積分
- 1058
- 點名
- 0
- 作業系統
- win7 32bit
- 軟體版本
- Office 2016 64-bit
- 閱讀權限
- 50
- 性別
- 男
- 來自
- 桃園
- 註冊時間
- 2012-5-9
- 最後登錄
- 2022-9-28
|
6#
發表於 2016-3-8 11:09
| 只看該作者
回復 1# boblovejoyce
草草完成如下程式,
因為VB的正則表達式功能弱了點
所以又自己寫了 FindCloseBracket 找出匹配的右括號位置- Sub Test()
- Dim fn As Integer, allText As String, indexClose As Long
- Dim fname
- fname = Application.GetOpenFilename()
- If TypeName(fname) = "Boolean" Then Exit Sub
- fn = FreeFile()
- Open fname For Input As #fn
- allText = Input$(LOF(1), 1)
- Close #fn
-
- Sheets.Add
- Cells(1, 1).Resize(, 3) = Array("net", "check", "portRef")
- Dim oRegex As Object: Set oRegex = CreateObject("vbscript.regexp")
- Dim oMch As Object, oMch2 As Object, x, i As Long, j As Long, endindex As Long
- Dim hasVDD As Boolean, hasGND As Boolean
- With oRegex
- .Global = True
- .Pattern = "\(net\s+([^\s()]*)" '(net 開頭,捕捉下一個非空白或()的字組
- Set oMch = .Execute(allText)
-
- .Pattern = "\(portRef\s+([^\s()]*)" '(portRef 開頭,捕捉下一個非空白或()的字組
- i = 2
- For Each x In oMch
- endindex = FindCloseBracket(allText, x.firstIndex)
- Set oMch2 = .Execute(Mid(allText, x.firstIndex, endindex - x.firstIndex + 1))
- Cells(i, 1) = x.subMatches(0)
- j = 3
- hasVDD = False: hasGND = False
- For Each y In oMch2
- If InStr(1, y.subMatches(0), "VDD", vbTextCompare) Then hasVDD = True
- If InStr(1, y.subMatches(0), "GND", vbTextCompare) Then hasGND = True
- Cells(i, j) = y.subMatches(0)
- j = j + 1
- Next
- Cells(i, 2) = IIf(oMch2.Count < 2 Or (hasVDD And hasGND), "X", "O")
-
- i = i + 1
- Next
- End With
- End Sub
- ' s: input string
- ' i: index of begin bracket "("
- Function FindCloseBracket(ByRef s As String, ByVal i As Long) As Long
- Dim isInString As Boolean 'ex : 預防雙引號字串內的 () 誤判
- i = i + 1 '從下一個字元開始
- Do While i <= Len(s)
- Select Case Mid(s, i, 1)
- Case "("
- If Not isInString Then
- i = FindCloseBracket(s, i)
- If i = 0 Then Exit Do
- End If
- Case ")"
- If Not isInString Then
- FindCloseBracket = i
- Exit Function
- End If
- Case """"
- isInString = Not isInString
- Case Else
- End Select
-
- i = i + 1
- DoEvents
- Loop
- FindCloseBracket = 0
- End Function
複製代碼 |
|