Excel - 我需要从字符串中提取数字和字母的模式

时间:2017-03-30 12:20:16

标签: excel

我有一长串文本,其中包含字符串中的某个地方,一个或两个数字,后跟1到3个字母。我需要将数字和字母一起提取到一个单独的单元格中。如果有这种模式的多个实例,我希望excel在用逗号分隔的同一个单元格中显示它们。

例如:

A1:在2G位置发现了故障。 B1:2G

A1:如果是32AB,则顶部缺失。 B1:32AB

A1:在2G位置发现了故障。在32AB的情况下,顶部缺失。 B1:2G,32AB。

我相信我在这里找出了基本情况:

Function ExtractPos(ByVal text As String) As String

Dim result As String
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")

RE.Pattern = "(\d{1,2}\w{1,4})"
RE.Global = True
RE.IgnoreCase = True
Set allMatches = RE.Execute(text)

If allMatches.Count <> 0 Then
    result = allMatches.Item(0).SubMatches.Item(0)
End If

ExtractPos = result

End Function

这似乎适用于返回多个结果,其中在函数调用中指定了分隔符:

    Function RegexExtract(ByVal text As String, _
                      Optional seperator As String = "") As String

Dim i As Long, j As Long
Dim result As String
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")

RE.Pattern = "(\d{1,2}\w{1,4})"
RE.Global = True
Set allMatches = RE.Execute(text)

For i = 0 To allMatches.Count - 1
    For j = 0 To allMatches.Item(i).submatches.Count - 1
        result = result & seperator & allMatches.Item(i).submatches.Item(j)
    Next
Next

If Len(result) <> 0 Then
    result = Right(result, Len(result) - Len(seperator))
End If

RegexExtract = result

End Function

我仍在尝试确定如何消除与此模式匹配的任何值:

"(\d{2}JAN|\d{2}FEB|\d{2}MAR|\d{2}APR|\d{2}MAY|\d{2}JUN|\d{2}JUL|\d{2}AUG|\d{2}SEP|\d{2}OCT|\d{2}NOV|\d{2}DEC)"

0 个答案:

没有答案