|
楼主 |
发表于 2014-4-13 20:46
|
显示全部楼层
本帖最后由 yyyydddd8888 于 2014-4-13 21:01 编辑
学习几位老师的好方法,利用submatches写了一个正则表达式来获取多个子匹配:
正则子匹配选择多项.zip
(9.69 KB, 下载次数: 15)
- Option Explicit
- Sub 提取()
- Dim arr, brr(1 To 100000, 1 To 4), reg As Object, i&, mat As Object, st$
- arr = Sheet1.Range("a2:a" & Range("a" & Rows.Count).End(3).Row)
- Set reg = CreateObject("vbscript.regexp")
- st = "(\d+.*?)([A-Z]\d{3,4})(\d{0,2}[A-Z]+)"
- With reg
- .ignorecase = False
- For i = 1 To UBound(arr)
- ' .Pattern = "(^.+?)[A-Z].+[A-Z](.+$)"
- ' Set mat = .Execute(arr(i, 1))
- ' brr(i, 1) = mat(0).submatches(0)
- ' brr(i, 4) = mat(0).submatches(1)
- ' Set mat = Nothing
- If Len(arr(i, 1)) > 20 Then
- st = "(^.*?)([A-Z]\d{4})(\d?[A-Z]+)(.*$)"
- Else
- st = "(^.*?)([A-Z]\d{3})(\d{2}[A-Z]+)(.*$)"
- End If
- .Pattern = st
- Set mat = .Execute(arr(i, 1))(0)
- brr(i, 1) = mat.submatches(0)
- brr(i, 2) = mat.submatches(1)
- brr(i, 3) = mat.submatches(2)
- brr(i, 4) = mat.submatches(3)
- Set mat = Nothing
- Next i
- End With
- Sheet1.Range("b2").Resize(i + 5, 4) = ""
- Sheet1.Range("b2").Resize(i - 1, 4) = brr
- End Sub
复制代码
|
|