简体   繁体   English

使用VBA从文本中提取字符串

[英]Extract string from text using VBA

I have an excel sheet with URLs that are a dumped from the server, I have the task of matching certain patterns and extracting the IDs, I am not an expert in RegExp but have got a start. 我有一个带有从服务器转储的URL的Excel工作表,我的任务是匹配某些模式并提取ID,我不是RegExp的专家,但已经有了一个起点。

Can you please look at the example below and let me know how best to take the IDs out, Thanks in advance 能否请您看下面的示例,并让我知道如何最好地提取ID,请提前谢谢

(" https://abcd.com/000001","Goods1 ") (“ https://abcd.com/000001","Goods1 ”)

Here I need to take out 000001 在这里我需要拿出000001

Function ExtractIds(urlstr As String)
    Dim reg
    Dim rng As Range, i As Long, j As Long
    Dim mtch, mt As String
    Set reg = CreateObject("vbscript.regexp")
    With reg
        .IgnoreCase = True
        .MultiLine = False
        .Pattern = "/(.*?)"""
        .Global = False
    End With
    MsgBox reg.Test(tmpStr)
    If reg.Test(tmpStr) Then
        ExtractIds = reg.Execute(tmpStr)(0).SubMatches(0)
    End If
End Function

How about: 怎么样:

Public Function ExtractIds(urlstr As String)
   ary = Split(urlstr, "*")
   ExtractIds = ary(2)
End Function

在此处输入图片说明

EDIT#1: 编辑#1:

Based on your Edit use this instead: 根据您的编辑,改用以下代码:

Public Function ExtractIds(urlstr As String)
   Dim DQ As String
   DQ = Chr(34)
   ary = Split(urlstr, DQ)
   bry = Split(ary(1), "/")
   ExtractIds = bry(UBound(bry))
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM