【发布时间】:2015-10-19 13:17:10
【问题描述】:
这是Regular Expressions in Excel VBA的扩展
我想出了我认为超出我最初问题范围的其他匹配项。这是我现有的代码:
Sub ImportFromDTD()
Dim sDTDFile As Variant
Dim ffile As Long
Dim sLines() As String
Dim i As Long
Dim Reg1 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Dim myRange As Range
Set Reg1 = New RegExp
ffile = FreeFile
sDTDFile = Application.GetOpenFilename("DTD Files,*.XML", , _
"Browse for file to be imported")
If sDTDFile = False Then Exit Sub '(user cancelled import file browser)
Open sDTDFile For Input Access Read As #ffile
Lines = Split(Input$(LOF(ffile), #ffile), vbNewLine)
Close #ffile
Cells(1, 2) = "From DTD"
J = 2
For i = 0 To UBound(Lines)
'Debug.Print "Line"; i; "="; Lines(i)
With Reg1
.Pattern = "\<\!ELEMENT\s+(\w+)\s+\((#\w+|(\w+)\+)\)\s+\>"
.Global = True
.MultiLine = True
.IgnoreCase = False
End With
If Reg1.Test(Lines(i)) Then
Set M1 = Reg1.Execute(Lines(i))
For Each M In M1
sExtract = M.SubMatches(2)
If Len(sExtract) = 0 Then sExtract = M.SubMatches(0)
sExtract = Replace(sExtract, Chr(13), "")
Cells(J, 2) = sExtract
J = J + 1
'Debug.Print sExtract
Next M
End If
Next i
Set Reg1 = Nothing
End Sub
这是我文件的摘录:
<!ELEMENT ProductType (#PCDATA) >
<!ELEMENT Invoices (InvoiceDetails+) >
<!ELEMENT Deal (DealNumber,DealType,DealParties) >
<!ELEMENT DealParty (PartyType,CustomerID,CustomerName,CentralCustomerID?,
LiabilityPercent,AgentInd,FacilityNo?,PartyReferenceNo?,
PartyAddlReferenceNo?,PartyEffectiveDate?,FeeRate?,ChargeType?) >
<!ELEMENT Deals (Deal*) >
目前,我正在匹配:
extract ProductType
<!ELEMENT ProductType (#PCDATA) >
extract InvoiceDetails
<!ELEMENT Invoices (InvoiceDetails+) >
我还需要提取以下内容:
Extract Deal
<!ELEMENT Deal (DealNumber,DealType,DealParties) >
Extract DealParty the ?,CR are throwing me off
<!ELEMENT DealParty (PartyType,CustomerID,CustomerName,CentralCustomerID?,
LiabilityPercent,AgentInd,FacilityNo?,PartyReferenceNo?,
PartyAddlReferenceNo?,PartyEffectiveDate?,FeeRate?,ChargeType?) >
Extract Deal
<!ELEMENT Deals (Deal*) >
【问题讨论】:
-
@pnuts 我认为您的编辑比我原来的问题更糟糕。
-
这个问题与我的第一个问题几乎完全相同,但更详细地说,我得到了 4 个赞成票。我有 2 票否决票,这一张票是关闭的吗?这在什么宇宙中有意义?