|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub test2()
Dim regex As Object
Dim mat As Object
Dim match As Object
Dim text As String
Dim pattern As String
Dim i As Integer
'表达式 = "[\u4e00-\u9fff]+*\d+(\.\d+)?" '编绳*2
表达式 = "[\u4e00-\u9fff]+*\d"
' 创建正则表达式对象
lastRow1 = Sheets("数据清洗").Cells.Find("*", , , , xlByRows, xlPrevious).Row
lastRow2 = Sheets("输出结果").Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1
Set regex = CreateObject("VBScript.RegExp")
With regex
.Global = True ' 搜索整个字符串
.pattern = 表达式 ' 设置模式
.IgnoreCase = True ' 忽略大小写
.MultiLine = False ' 单行模式(不是多行)
End With
' 设置正则表达式的属性
' 执行匹配操作
For i = 2 To lastRow1
' 定义要搜索的文本
文本 = Sheets("数据清洗").Range("e" & i).Value
Set mat = regex.Execute(文本)
' 遍历所有匹配项并输出
For Each match In mat
If match Like "*编绳*" Then
inputString = match
编绳 = ExtractData(inputString, "编绳")
Sheets("输出结果").Range("k" & i) = 编绳
End If
If match Like "*边布*" Then
inputString = match
边布 = ExtractData(inputString, "边布")
Sheets("输出结果").Range("l" & i) = 边布
End If
If match Like "*胶带*" Then
inputString = match
胶带 = ExtractData(inputString, "胶带")
Sheets("输出结果").Range("m" & i) = 胶带
End If
If match Like "*袋子*" Then
inputString = match
袋子 = ExtractData(inputString, "袋子")
Sheets("输出结果").Range("n" & i) = 袋子
End If
Next match
Next
' 清理对象
Set match = Nothing
Set matches = Nothing
Set regex = Nothing
End Sub
|
-
|