|
本帖最后由 weiyingde 于 2024-9-22 11:03 编辑
将“解析”分枝的三项内容,按A、B、C、D的顺序调整。
代码如下:
Sub 要求匹配解析捕获四个()
Dim mt, mk, oRng As Range, n&, m&, odoc As Document
Dim str$, tt$, arr() As Range, x%
Dim rg As Range, dic As Object, k&, ph As Paragraph
Set odoc = ThisDocument
Set dic = CreateObject("Scripting.Dictionary")
osr = odoc.Content
'************************
str = Replace(osr, Chr(1), "") '替换控件
Set ph = odoc.Paragraphs(1)
Set wt = ph.Range.Words(ph.Range.Words.Count) '注意不要-1
r = wt.End - Len(ph.Range.Text) + (Len(osr) - Len(str))
'*************************
str = Replace(str, Chr(7), "") 'osr改str
With CreateObject("vbscript.regexp")
.Global = True
.Ignorecase = False
.MultiLine = True
.Pattern = "(^解析[^\r]*)(([A-D](?:(?![A-D\r]).)*){3})\r"
For Each mt In .Execute(str)
m = mt.FirstIndex + r: n = mt.Length
Set oRng = odoc.Range(m, m + n - 1)
ixsr = mt.submatches(0)
jxlth = Len(ixsr)
.Pattern = "([A-D])([^A-D\r]+)[,,;。;]"
For Each mtt In .Execute(oRng)
'm2 = mtt.FirstIndex + m: n2 = mtt.Length
'Set oRng2 = odoc.Range(m2, m2 + n2)
isr2 = IIf(InStr(",,;。;", Right(mtt, 1)) > 0, Left(mtt, Len(mtt) - 1), mtt)
nbr = WorksheetFunction.Find(Left(mtt, 1), "ABCD")
dic(Val(nbr)) = isr2
Next
ky = dic.keys
ky = SortAry(ky)
For i = LBound(ky) To UBound(ky)
srth = srth & dic(ky(i)) & IIf(i < UBound(ky), ";", "。")
Next
odoc.Range(m + jxlth, m + n - 1) = srth
dic.RemoveAll
srth = ""
Next
End With
End Sub
Function SortAry(arr As Variant) As Variant
Dim i As Long
Dim j As Long
Dim temp As Variant
' 对数组进行升序排序
For i = LBound(arr) To UBound(arr) - 1
For j = i + 1 To UBound(arr)
If arr(i) > arr(j) Then
' 交换元素
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
End If
Next j
Next i
' 返回排序后的数组
SortAry = arr
End Function
问题:可能锚定位置不准确,导致乱版和字符重复。
D:\1 |
|