0

Although there is already lots of regex topic, I couldn't find a good solution to split a string like this: 12ABC34DE102/103/104FG

Result should be: 12ABC34DE102FG | 12ABC34DE103FG | 12ABC34DE104FG

EDIT: Other example: 12ABC34DE102/103AH001/AH002 should be 12ABC34DE102AH001 | 12ABC34DE103AH001 | 12ABC34DE102AH002 | 12ABC34DE103AH002

With this regex: ([0-9]{2,3})*\/([0-9]{2,3})*, I'm able to get into groups the changing values but I cannot go further.

Thank you

1 Answer 1

2

I feel like you may want to first capture the forward slash delimited substring and then use Split() to create an array to go over. Something like so:

Sub Test()

Dim str As String: str = "12ABC34DE102/103/104FG"
Dim arr() As String, x As Long

With CreateObject("vbscript.regexp")
    .Pattern = "\d{2,3}(?:/\d{2,3})+"
    If .Test(str) Then
        arr = Split(.Execute(str)(0), "/")
        For x = LBound(arr) To UBound(arr)
            arr(x) = .Replace(str, arr(x))
        Next
    End If
End With

End Sub

Running this will end up with:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Great, I like that!
Hello, thank you but I forgot one condition and your solution is not working in this case. I edited the question, there could be several slashes with different pattern (the pattern is not the issue but the additional slash is because it cannot be use in the split then)
@Kokno, that would complicate the matter. The above gave you a good idea of what to look for. Try to adapt the code, do your research. I'm not going to keep writing code for you I'm afraid.
@JvdV yes your answer already helped me and I adapted it to the new case, thank you

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.