Excel从括号中提取
如果单元格只包含一个“(文本)”,那么从括号中获取值的代码就可以正常工作。
不幸的是,在我的行中有很多“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”。 谢谢。
为什么要打扰正则expression式 ? 考虑另一种方法:
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
编辑#1:
更简单:
Public Function GetParen2(strIn As String) As String ary = Split(strIn, "(") bry = Split(ary(UBound(ary)), ")") GetParen2 = bry(0) End Function
tiborjan:
我有一个function,我只是这样写的。 这里是:
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)
结束function
它有5个参数:
- s1:包含要分析的文本的string
- sLeft:要分析的文本的左边界。 使用“(”为您的应用程序。
- sL_Occ:sLeft的迭代次数。 所以如果你想要第二个“(”出现在S1中,使这2。
- sRight:和sLeft一样,但是是右边的。
- sR_Occ:与sL_Occ相同,但是指的是右边的。
为了简单起见,请使用此function代码而非您的function代码。 如果要从第二组圆括号中提取文本,请使用
s1 =“(P1)(P2)(P3)(P4)”
sLeft =“(”
sL_Occ = 2
sRight =“)”
sR_Occ = 2
上面的回报将是“P2”。
希望有所帮助! 马特,通过ExcelArchitect.com
或者简单地说
Function LastParam(ByVal str As String) As String Dim arr() As String arr = Split(str, "(") LastParam = Split(arr(UBound(arr, 1)), ")")(0) End Function
为了完整起见,您只需对代码进行小的更改即可使其与正则expression式一起工作。
将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
对我之前的正则Regexp
小小的调整将会提取最后一场比赛。
testing
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