从括号中提取Excel

时间:2015-03-07 12:37:35

标签: excel vba excel-vba excel-formula

Get the value between the brackets的代码如果单元格只包含一个"(文本)"则效果很好。

不幸的是,在我的行中有很多" Sample(sample1)(sample2)"格式句子,我需要最后一部分。

Function GetParen(strIn As String) As String
Dim objRegex As Object
Dim objRegMC As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
    .Pattern = "\((.+?)\)"
    If .Test(strIn) Then
        Set objRegMC = .Execute(strIn)
        GetParen = objRegMC(0).submatches(0)
    Else
        GetParen = "No match"
    End If
End With
Set objRegex = Nothing
End Function

有人可以帮我修改代码吗?因为如果单元格包含"文本(文本part1)(文本part2)"我得到的结果是"文本part1"但是我需要"文本part2"。 感谢。

5 个答案:

答案 0 :(得分:1)

为什么要打扰正则表达式?考虑替代方案:

Public Function GetParen(strIn As String) As String
    Dim gather As Boolean, L As Long, i As Long
    Dim CH As String
    gather = False
    L = Len(strIn)
    For i = L To 1 Step -1
        CH = Mid(strIn, i, 1)
        If gather Then GetParen = CH & GetParen
        If CH = ")" Then gather = True
        If CH = "(" Then Exit For
    Next i
    GetParen = Mid(GetParen, 2)
End Function

enter image description here

修改#1:

简单:

Public Function GetParen2(strIn As String) As String
    ary = Split(strIn, "(")
    bry = Split(ary(UBound(ary)), ")")
    GetParen2 = bry(0)
End Function

答案 1 :(得分:1)

tiborjan:

我有一个我为此写的功能。这是:

Function SubStr(s1 As String, sLeft As String, sL_Occ As Integer, sRight As String, sR_Occ As Integer) As String
'Cuts a piece of text from between two strings within another string
Dim LeftBound As Integer, RightBound As Integer, i As Integer

If sLeft = "Minimum" Then
    LeftBound = 0
Else
    For i = 1 To sL_Occ
        LeftBound = InStr(LeftBound + 1, s1, sLeft, vbTextCompare)
    Next i
End If

LeftBound = LeftBound + Len(sLeft) - 1  'measure from the right of the left bound

If sRight = "Maximum" Then
    RightBound = Len(s1) + 1
Else
    For i = 1 To sR_Occ
        RightBound = InStr(RightBound + 1, s1, sRight, vbTextCompare)
    Next i
End If

SubStr = Mid(s1, LeftBound + 1, RightBound - LeftBound - 1)

结束功能

它有5个参数:

  1. s1:包含要解析的文本的字符串
  2. sLeft:要解析的文本的左边界。使用“(”表示您的申请。
  3. sL_Occ:sLeft的迭代次数。因此,如果您想要第二个“(”出现在s1中,请将其设为2。
  4. sRight:与sLeft相同,但是右边界。
  5. sR_Occ:与sL_Occ相同,但是指的是右边界。
  6. 为了方便起见:使用此功能代码而不是您的功能代码。如果要从第二组括号中提取文本,请使用

    s1 =“(P1)(P2)(P3)(P4)”

    sLeft =“(”

    sL_Occ = 2

    sRight =“)”

    sR_Occ = 2

    上面的回报是“P2”。

    希望有所帮助! 马特,通过ExcelArchitect.com

答案 2 :(得分:1)

或者如何简单地

Function LastParam(ByVal str As String) As String
Dim arr() As String

    arr = Split(str, "(")
    LastParam = Split(arr(UBound(arr, 1)), ")")(0)
End Function

答案 3 :(得分:0)

为了完整起见,您只需对代码进行细微更改即可使其与正则表达式一起使用。

将Global标记设置为True,并返回匹配集合中的最后一个匹配项。

Function GetParen(strIn As String) As String
Dim objRegex As Object
Dim objRegMC As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
    .Global = True
    .Pattern = "\((.+?)\)"
    If .Test(strIn) Then
        Set objRegMC = .Execute(strIn)
        GetParen = objRegMC(objRegMC.Count - 1).submatches(0)
    Else
        GetParen = "No match"
    End If
End With
Set objRegex = Nothing
End Function

答案 4 :(得分:0)

对我之前Regexp的小调整将提取最后一场比赛。

测试

Sub Test()
MsgBox GetParen("(Sample (sample1) (sample2)")
End Sub

Function GetParen(strIn As String) As String
Dim objRegex As Object
Dim objRegMC As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
    .Pattern = "\((.+?)\)"
    .Global = True
    If .Test(strIn) Then
        Set objRegMC = .Execute(strIn)
        GetParen = objRegMC(objRegMC.Count - 1).submatches(0)
    Else
        GetParen = "No match"
    End If
End With
Set objRegex = Nothing
End Function