- 帖子
- 1018
- 主題
- 15
- 精華
- 0
- 積分
- 1058
- 點名
- 0
- 作業系統
- win7 32bit
- 軟體版本
- Office 2016 64-bit
- 閱讀權限
- 50
- 性別
- 男
- 來自
- 桃園
- 註冊時間
- 2012-5-9
- 最後登錄
- 2022-9-28
|
13#
發表於 2016-3-11 19:39
| 只看該作者
本帖最後由 stillfish00 於 2016-3-11 19:42 編輯
回復 12# boblovejoyce
恩,firstIndex 這邊我錯了,match到第一個字時 firstIndex = 0,和VBA不一樣,難怪跑那麼久,修改如下
然後有些rename的我不知要取哪個名稱,都寫到第一欄- Sub Test()
- '== Read File ==
- Dim fn As Integer, allText As String
- 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
-
- '== Parse Element ==
- Dim oRegex As Object: Set oRegex = CreateObject("vbscript.regexp")
- Dim oDic As Object: Set oDic = CreateObject("scripting.dictionary")
- 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+(\(rename.*\)|[^\s()]*)" '(net 開頭,捕捉下一個非空白或()的字組或rename...
- Set omch = .Execute(allText)
-
- .Pattern = "\(portRef\s+([^\s()]*)" '(portRef 開頭,捕捉下一個非空白或()的字組
- i = 1
- For Each x In omch
- endIndex = FindCloseBracket(allText, x.firstIndex + 1)
- If endIndex = 0 Then MsgBox "Fail to parse: " & vbNewLine & Mid(allText, (x.firstIndex + 1), 50) & "...": Exit Sub
- Set oMch2 = .Execute(Mid(allText, x.firstIndex + 1, endIndex - (x.firstIndex + 1) + 1))
-
- 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
- Next
-
- If oMch2.Count < 2 Or (hasVDD And hasGND) Then 'portRef低於兩個或同時含VDD及GND字串
- oDic.Add i, x.subMatches(0)
- i = i + 1
- End If
- Next
- End With
-
- '== Output result ==
- Dim ar
- If oDic.Count = 0 Then MsgBox "All pass": Exit Sub
- ar = oDic.items
- With Sheets.Add
- Application.ScreenUpdating = False
- .[a1].Resize(UBound(ar) - LBound(ar) + 1) = OneDtoTwoD(ar)
- Application.ScreenUpdating = True
- 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
- Loop
- FindCloseBracket = 0
- End Function
- Function OneDtoTwoD(ar, Optional base As Integer) As Variant 'transepose array of elements > 65535
- Dim i, retn()
- ReDim retn(base To base + UBound(ar) - LBound(ar), base To base) ' transe to base
- For i = LBound(ar) To UBound(ar)
- retn(i - LBound(ar) + base, base) = ar(i)
- Next
- OneDtoTwoD = retn
- End Function
複製代碼 |
|