How to extract ad sizes from a string with excel regex

前端 未结 3 1011
野性不改
野性不改 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:22

    EDIT: Since it's not actually underscore delimited we can't use Split. We can however iterate over the string and extract the "#x#" manually. I have updated the code to reflect this and verified that it works successfully.

    Public Function ExtractAdSize(ByVal arg_Text As String) As String
    
        Dim i As Long
        Dim Temp As String
        Dim Ad As String
    
        If arg_Text Like "*#x#*" Then
            For i = 1 To Len(arg_Text) + 1
                Temp = Mid(arg_Text & " ", i, 1)
                If IsNumeric(Temp) Then
                    Ad = Ad & Temp
                Else
                    If Temp = "x" Then
                        Ad = Ad & Temp
                    Else
                        If Ad Like "*#x#*" Then
                            ExtractAdSize = Ad
                            Exit Function
                        Else
                            Ad = vbNullString
                        End If
                    End If
                End If
            Next i
        End If
    
    End Function
    

    Alternate version of the same function using Select Case boolean logic instead of nested If statements:

    Public Function ExtractAdSize(ByVal arg_Text As String) As String
    
        Dim i As Long
        Dim Temp As String
        Dim Ad As String
    
        If arg_Text Like "*#x#*" Then
            For i = 1 To Len(arg_Text) + 1
                Temp = Mid(arg_Text & " ", i, 1)
    
                Select Case Abs(IsNumeric(Temp)) + Abs((Temp = "x")) * 2 + Abs((Ad Like "*#x#*")) * 4
                    Case 0: Ad = vbNullString       'Temp is not a number, not an "x", and Ad is not valid
                    Case 1, 2, 5: Ad = Ad & Temp    'Temp is a number or an "x"
                    Case 4, 6: ExtractAdSize = Ad   'Temp is not a number, Ad is valid
                               Exit Function
                End Select
            Next i
        End If
    
    End Function
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-24 16:32

    This formula could work if it's always 3 characters, then x, and it's always between underscores - adjust accordingly.

    =iferror(mid(A1,search("_???x*_",A1)+1,search("_",A1,search("_???x*_",A1)+1)-(search("_???x*_",A1)+1)),"No match")
    
    0 讨论(0)
提交回复
热议问题