如何从连续文本

时间:2017-05-13 03:22:01

标签: regex excel excel-vba excel-formula textjoin vba

我在excel中有一个列,其中包含一个用“;”划分的连接字符串e.g

  

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。我该如何实现这一目标?这里有数组公式的机会吗?

3 个答案:

答案 0 :(得分:2)

如果您订阅了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会将{}放在公式周围。

enter image description here

如果您没有订阅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

答案 1 :(得分:0)

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

static int index = 0;
index = 10;

用于B2:B3 as,

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

enter image description here

答案 2 :(得分:0)

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

enter image description here

单元格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&";"&C1&";"&D1&";"&E1&";"&F1