|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub 正则提取()
- Dim data
- data = readfile(ThisWorkbook.Path & "\2023.txt")
- Set reg = CreateObject("vbScript.Regexp")
- reg.Global = True
- reg.Pattern = "(h1|p>\d+-\d+)"
- ar = Split(data, Chr(10))
- For i = 0 To UBound(ar) '海选,保留 h1 以及 p 数字-数字 的行
- If reg.test(ar(i)) Then
- n = n + 1
- s = s & ar(i)
- End If
- Next
- reg.Pattern = ".*?<\w+>((\d+-\d+(?=<))|农历(.*?)(?=年)|(闰.*?(?=月))?.*?月([大小]))|.+"
- s = Split(Trim(reg.Replace(s, "$2$3$4$5 "))) '精选,保留农历年、大年初一阳历以及闰月并重新排列
- t = "": r = ">>"
- For i = 0 To UBound(s)
- t = Mid(s(i), 1, 1)
- If t Like "#" Then
- If y = "" Then y = s(i) '大年初一的阳历
- ElseIf t = "闰" Then
- r = Mid(s(i), 2, 1) & ">" & Right(s(i), 1) '闰月份
- Else
- sn = sn & ">" & s(i)
- End If
- Next
- sn = sn & ">" & r & ">" & y
- MsgBox Mid(sn, 2)
- End Sub
- '读取文件
- Function readfile(ByVal filename As String, Optional filetype As String = "utf-8")
- Dim ReadStream As Object
- Set ReadStream = CreateObject("ADODB.Stream")
- Dim FileContent As String
- With ReadStream
- .Type = 2 'adTypeText
- .Charset = filetype
- .Open
- .LoadFromFile filename
- FileContent = .ReadText
- .Close
- End With
- readfile = FileContent
- End Function
复制代码 |
评分
-
2
查看全部评分
-
|