Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88809

Can I use regex to not only identify a pattern but extract the value found?

$
0
0

I am using Excel VBA.

I need to extract the dimensions (width x height) of a creative from a string and the dimensions will always be in the format:

000x000 or 000X000 or 000x00 or 000X00 where 0 can be any number between 1-9 and x can be upper or lower case.

I read this guide: How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

And I think what I want is something similar to:

[0-9]{2, 3}[xX][0-9]{2, 3}

So if my string is:

creativeStr = ab234-cdc-234-300x250-777aabb

I want to extract "300x250" and assign it to a variable like this:

dimensions = 300x250

Is my Regex above correct? Also, how would I pull the resulting match into a variable?

Here is part of my code:

creativeStr = "Sample-abc-300x250-cba-123"
regex_pattern = "[0-9]{2,3}[xX][0-9]{2,4}"

If regex_pattern <> "" Then

    With regEx
        .Global = True
        .Pattern = regex_pattern
    End With

    If regEx.Test(creativeStr) Then
        dimensions = regEx.Replace(creativeStr, "$1")
    Else
        dimensions = "Couldn't extract dimensions from creative name."
    End If

End If

But it still returns the condition in my else clause...

Thanks!


Viewing all articles
Browse latest Browse all 88809

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>