从字符串中提取一系列参考编号

Extract a series of reference numbers from a string

我在数据库中有一个包含用户评论的字段。

这些评论有时包含订单参考号、送货参考号或承运人参考号。订单参考遵循格式

这些引用被用户评论中的其他信息包围,因此零星出现,没有固定模式。

示例数据:"Item lost due to order duplication X1234567890 possibly also due to theft"

我想做的是在我的 table 中创建 3 个额外的列,每个引用一个,然后搜索用户评论并将这些引用提取到他们的正确列中。

这将在 Access 中执行,但在 excel 中也需要。

任何人都可以帮助使用 Visual Basic 函数来执行此操作吗?

请帮忙!我一直在拔头发!

您可以使用正则表达式。

以下函数 returns VBA.Collection 对。对的第一项包含数字类型(X 或 TR 或 FC),第二项包含数字。

Public Function ParseComment(comment As String) As VBA.Collection

    Set ParseComment = New VBA.Collection

    Static RegExp As Object
    If RegExp Is Nothing Then
        Set RegExp = CreateObject("VBScript.RegExp")
            RegExp.IgnoreCase = True
            RegExp.Pattern = "\b(X|TR|FC)(\d+)\b"
            RegExp.Global = True
    End If

    Dim match As Object
    For Each match In RegExp.Execute(comment)
        ParseComment.Add Array(match.SubMatches(0), match.SubMatches(1))
    Next match

End Function