1

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
1
  • Is the pattern always like what you have mentioned? https://abcd.com/ followed by the ID? Commented May 21, 2015 at 11:47

1 Answer 1

2

How about:

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

enter image description here

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
Sign up to request clarification or add additional context in comments.

3 Comments

There was a typo in my post. ID is not surrounded by *. I have updated.
@Gary'sStudent Awesome answer! Love the creative aspect of it and the simplicity.
@Gary'sStudent I must say you are very much creative persion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.