How to extract ad sizes from a string with excel regex

前端 未结 3 1018
野性不改
野性不改 2021-01-24 15:52

I am trying to extract ad sizes from string. The ad sizes are all set standard sizes. So while I\'d prefer to have a regex that looks for a pattern, IE 3 numbers followed by 2 o

3条回答
  •  时光取名叫无心
    2021-01-24 16:26

    I have managed to make about 95% of the required answer - the RegEx below will remove the DDDxDD size and would return the rest.

    Option Explicit
    
    Public Function regExSampler(s As String) As String
    
        Dim regEx           As Object
        Dim inputMatches    As Object
        Dim regExString     As String
    
        Set regEx = CreateObject("VBScript.RegExp")
    
        With regEx
            .Pattern = "(([0-9]+)x([0-9]+))"
            .IgnoreCase = True
            .Global = True
    
            Set inputMatches = .Execute(s)
    
            If regEx.test(s) Then
                regExSampler = .Replace(s, vbNullString)
            Else
                regExSampler = s
            End If
    
        End With
    
    End Function
    
    Public Sub TestMe()
    
        Debug.Print regExSampler("uni3uios3_300x250_ASDF.html")
        Debug.Print regExSampler("uni3uios3_34300x25_ASDF.html")
        Debug.Print regExSampler("uni3uios3_8x4_ASDF.html")
    
    End Sub
    

    E.g. you would get:

    uni3uios3__ASDF.html
    uni3uios3__ASDF.html
    uni3uios3__ASDF.html
    

    From here you can continue trying to find a way to reverse the display.

    Edit: To go from the 95% to the 100%, I have asked a question here and it turns out that the conditional block should be changed to the following:

    If regEx.test(s) Then
        regExSampler = InputMatches(0)
    Else
        regExSampler = s
    End If
    

提交回复
热议问题