VBA 正则表达式初体验
Option ExplicitSub demo()Dim i As Long, j As Long, k As Long, s As StringDim reg1 As Object, matches1 As Object, mch1 As ObjectDim reg2 As Object, matches2 As Object, mch2 As ObjectDim allBonus As Stringi = 6s = Range("a1").ValueSet reg1 = CreateObject("vbscript.regexp")reg1.Global = Truereg1.Pattern = "姓名:\s*(\S+)\s*获奖情况:([^;]+);"Set reg2 = CreateObject("vbscript.regexp")reg2.Global = Truereg2.Pattern = "\s*(\S+)奖(\S+)"Set matches1 = reg1.Execute(s)For Each mch1 In matches1Cells(i, 2) = mch1.submatches(0)allBonus = Trim(mch1.submatches(1))Set matches2 = reg2.Execute(allBonus)For Each mch2 In matches2For k = 3 To 6If Cells(5, k) = mch2.submatches(0) ThenCells(i, k) = mch2.submatches(1)End IfNext kNext mch2i = i + 1Next mch1
End Sub