|
楼主 |
发表于 2019-11-17 14:23
|
显示全部楼层
当前2问,看来只要正则的基础知识扎实,就能正确解答。
我把自己对这2问的完整解答也放上来吧。
- Option Explicit
- Private Const 正则1$ = "小计\d*\.?\d+" '题1_填空处"
- Private Const 正则2$ = "(?![月计])[一-龥]\d*\.?\d+" '题2_填空处
- Sub Test1()
- Dim r&, ar, Reg As Object, br, ss$, mas As Object, ma As Object, v#, Start%, Length%, i&
- r = Cells(Rows.Count, 1).End(xlUp).Row
- With Range("A1").Resize(r)
- .Font.Bold = False
- .Font.Color = 0
- ar = .Resize(, 2).Value
- End With
- ReDim br(2 To r, 1 To 1)
- Set Reg = CreateObject("VBScript.RegExp")
- With Reg
- .Global = True
- .Pattern = 正则1
- For i = 2 To r
- ss = ar(i, 1) '题1_填空处2
- Set mas = .Execute(ss)
- For Each ma In mas
- Start = ma.firstindex + 3 '题1_填空处3
- Length = ma.Length - 2 '题1_填空处4
- v = Mid(ma.Value, 3) '题1_填空处5
- br(i, 1) = br(i, 1) + v
- With Range("A" & i).Characters(Start, Length).Font
- .Bold = True
- .Color = vbBlue
- End With
- Next
- Next
- End With
- Range("B2").Resize(r - 1) = br
- End Sub
- Sub Test2()
- Dim r&, ar, Reg As Object, br, ss$, mas As Object, ma As Object, v#, Start%, Length%, i&
- r = Cells(Rows.Count, 1).End(xlUp).Row
- With Range("A1").Resize(r)
- ar = .Resize(, 2).Value
- End With
- ReDim br(2 To r, 1 To 1)
- Set Reg = CreateObject("VBScript.RegExp")
- With Reg
- .Global = True
- .Pattern = 正则2
- For i = 2 To r
- ss = ar(i, 1) '题2_填空处2
- Set mas = .Execute(ss)
- For Each ma In mas
- Start = ma.firstindex + 2 '题2_填空处3
- Length = ma.Length - 1 '题2_填空处4
- v = Mid(ma.Value, 2) '题2_填空处5
- br(i, 1) = br(i, 1) + v
- With Range("A" & i).Characters(Start, Length).Font
- .Bold = True
- .Color = vbRed
- End With
- Next
- Next
- End With
- Range("C2").Resize(r - 1) = br
- End Sub
复制代码
|
|