【问题标题】:How to extract the last forward slash(/) from a concatenated text如何从连接的文本中提取最后一个正斜杠(/)
【发布时间】:2017-05-13 03:22:00
【问题描述】:

我在 excel 中有一个列,其中包含一个由“;”分隔的连接字符串例如

SM/123456789/1;PM/123456789/21;AM/123456789/1;GM/123456789/81;QM/123456789/1

我想返回第二个正斜杠的值,例如

1;21;1;81;1

注意:我将使用 SM/123456789/199 为一个输入提取最后一个“/”

IF(ISERROR(FIND("/",B19)),"",RIGHT(B19,LEN(B19)-FIND("/",B19,FIND("/",B19)+1)))

这将提取199 或在SM/123456789/1 的情况下为1。我如何实现这一目标?这里有数组公式的机会吗?

【问题讨论】:

  • 抱歉,我没听懂@pnuts
  • vba 在循环或全局正则表达式中拆分 rwice;要么在 udf 中。
  • 这将需要以下三种之一:帮助列、vba 或 TEXTJOIN()(仅适用于 Office 365 Excel 订阅)。

标签: regex excel excel-formula textjoin vba


【解决方案1】:

如果您订阅了 Office 365 Excel,请使用以下数组公式:

=TEXTJOIN(";",TRUE,TRIM(MID(SUBSTITUTE(SUBSTITUTE(A1,";","/"),"/",REPT(" ",999)),(ROW(INDIRECT("1:" & LEN(A1)-LEN(SUBSTITUTE(A1,";",""))+1))*3-1)*999,999)))

作为数组公式,退出编辑模式时必须使用 Ctrl-Shift-Enter 而不是 Enter 来确认。如果操作正确,Excel 会在公式周围加上{}


如果您没有订阅 Office 365 Excel,您可以将此代码放在附加到工作簿的模块中,并使用上述公式。

Function TEXTJOIN(delim As String, skipblank As Boolean, arr)
    Dim d As Long
    Dim c As Long
    Dim arr2()
    Dim t As Long, y As Long
    t = -1
    y = -1
    If TypeName(arr) = "Range" Then
        arr2 = arr.Value
    Else
        arr2 = arr
    End If
    On Error Resume Next
    t = UBound(arr2, 2)
    y = UBound(arr2, 1)
    On Error GoTo 0

    If t >= 0 And y >= 0 Then
        For c = LBound(arr2, 1) To UBound(arr2, 1)
            For d = LBound(arr2, 1) To UBound(arr2, 2)
                If arr2(c, d) <> "" Or Not skipblank Then
                    TEXTJOIN = TEXTJOIN & arr2(c, d) & delim
                End If
            Next d
        Next c
    Else
        For c = LBound(arr2) To UBound(arr2)
            If arr2(c) <> "" Or Not skipblank Then
                TEXTJOIN = TEXTJOIN & arr2(c) & delim
            End If
        Next c
    End If
    TEXTJOIN = Left(TEXTJOIN, Len(TEXTJOIN) - Len(delim))
End Function

【讨论】:

    【解决方案2】:

    这是一个带有正则表达式的 UDF,可以选择替换分隔符。

    Option Explicit
    Option Base 0    '<~~this is the default but I've included it because it has to be 0
    
    Function mySplitString(str As String, _
                            Optional delim As String = ";")
        Dim n As Long, nums() As Variant
        Static rgx As Object, cmat As Object
    
        'with rgx as static, it only has to be created once; beneficial when filling a long column with this UDF
        If rgx Is Nothing Then
            Set rgx = CreateObject("VBScript.RegExp")
        End If
    
        mySplitString = vbNullString
    
        'make sure the str terminated in a semi-colon
        str = str & Chr(59)
    
        With rgx
            .Global = True
            .MultiLine = False
            .Pattern = "\d{1,3}[\;]{1}"
            If .Test(str) Then
                Set cmat = .Execute(str)
                'resize the nums array to accept the matches
                ReDim nums(cmat.Count - 1)
                'populate the nums array with the matches
                For n = LBound(nums) To UBound(nums)
                    nums(n) = cmat.Item(n)
                Next n
                'convert the nums array to a delimited string
                If delim <> Chr(59) Then
                    mySplitString = Replace(Join(nums, delim), Chr(59), vbNullString)
                Else
                    mySplitString = Join(nums, vbNullString)
                    mySplitString = Left(mySplitString, Len(mySplitString) - 1)
                End If
            End If
        End With
    End Function
    

    在 B2:B3 中用作,

    =mySplitString(A2, ",")
    =mySplitString(A3)
    

    【讨论】:

      【解决方案3】:

      如果您没有TEXTJOIN 功能,您可以尝试以下方法,但这种方法需要辅助列,

      单元格B1上的公式,

      =TRIM(RIGHT(SUBSTITUTE(TRIM(MID(SUBSTITUTE($A1,";",REPT(" ",999)),(COLUMN(A1)-1)*999+1,999)),"/",REPT(" ",999)),999))

      您必须将其拖动到column F (5 columns),因为输入字符串包含由; 分隔的5 个文本字符串。最后给column G添加一个公式,把;分隔的所有字符串串联起来,

      =B1&amp;";"&amp;C1&amp;";"&amp;D1&amp;";"&amp;E1&amp;";"&amp;F1

      【讨论】:

        猜你喜欢
        • 2022-06-23
        • 2017-10-10
        • 1970-01-01
        • 1970-01-01
        • 2022-10-23
        • 1970-01-01
        • 2019-09-19
        • 2016-07-30
        • 1970-01-01
        相关资源
        最近更新 更多