如何在Excel /电子表格中的两个字符之间获取字符串

时间:2019-06-12 05:21:52

标签: excel excel-formula

我有这个字符串

Weiss,Emery/Ap #519-8997 Quam. Street/Hawaiian Gardens,IN - 79589|10/13/2010

我如何只获得夏威夷花园?

我已经尝试过使用

=mid(left(A1,find("/",A1)-1),find(",",A1)+1,len(A1))

它给了我金刚砂

2 个答案:

答案 0 :(得分:2)

如果要提取的字符串前总是有两个斜线,那么根据Tyler M's答案,您可以使用此

=MID(E1,
     FIND("~",SUBSTITUTE(E1,"/","~",2))+1,
     FIND(",",RIGHT(E1,LEN(E1)-FIND("~",SUBSTITUTE(E1,"/","~",2))))-1
     )

这将/的第二次出现替换为通常不会在地址中出现的字符,从而使其可查找。

答案 1 :(得分:2)

您是否还打算包含Google Spreadsheets(查看标题)?如果是这样,您可以使用REGEXEXTRACT()函数。例如在B1

=REGEXEXTRACT(A1,"\/([\w\s]*)\,")

enter image description here

在Excel中,您可以使用以下正则表达式规则构建UDF(例如):

Function REGEXEXTRACT(S As String, PTRN As String) As String

'We will get the last possible match in your string...
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")
With regex
    .Pattern = PTRN
    .Global = True
End With

Set matches = regex.Execute(S)
For Each Match In matches
    If Match.SubMatches.Count > 0 Then
        For Each subMatch In Match.SubMatches
            REGEXEXTRACT = subMatch
        Next subMatch
    End If
Next Match

End Function

像这样调用B1中的函数:

=REGEXEXTRACT(A1,"\/([\w\s]*)\,")

enter image description here