RegEx Query返回第n个匹配

时间:2018-01-18 15:04:11

标签: regex vba ms-access

基本上,我在Access查询中有一个正则表达式,它从一长串文本中提取产品代码。每个字符串中最多有10个代码,我在查询中需要10个字段来提取十个代码中的每一个。

到目前为止,我只能通过更改Global = True|False来获取第一个或最后一个代码。我正在使用的代码如下:

Function extSKU( _
    Comments As Variant)

    Dim SKU_re As New RegExp
    SKU_re.Pattern = "\n\d{4,5}[A-Z]{0,1}"
    SKU_re.Global = False
    SKU_re.IgnoreCase = True
    Dim SKU_m
    For Each SKU_m In SKU_re.Execute(Comments)
      extSKU = Replace(Trim(UCase(SKU_m.Value)), "Completed By: ", "")
    Next
End Function

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

最终答案:这会拉出10个SKU中的第一个,并且我为每个SKU提供10个不同的函数,如果没有匹配,则IF返回null。

Function extSKU1( _
  Comments As Variant)
  Dim SKU_re As New RegExp
  SKU_re.Pattern = "\n\d{4,5}[A-Z]{0,1}\s{0,1}(\d{0,3})"
  SKU_re.Global = True
  SKU_re.IgnoreCase = True
  Dim SKU_m
  Dim SKU As New Collection
  For Each SKU_m In SKU_re.Execute(Comments)
    SKU.Add Trim(UCase(SKU_m.Value))
  Next
  extSKU1 = SKU(1)
End Function