简体   繁体   中英

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.

Can you please look at the example below and let me know how best to take the IDs out, Thanks in advance

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

Here I need to take out 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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