从Excel VBA中的字符串中提取最后一个括号字符 [英] Extract Last Bracketed Characters from String in Excel VBA
本文介绍了从Excel VBA中的字符串中提取最后一个括号字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的字符串是这样的文件名:
My strings are filenames like this:
示例文本(A12 V1.1)
Sample Text (A12 V1.1)
示例(V2)文本(A9 V2.3 8.99)
Sample (V2) Text (A9 V2.3 8.99)
样本超文本(A34 8.3 V4)
Sample Very Text (A34 8.3 V4)
如何仅提取以括号开头但不包含"V"的字符串?
How do I extract only the string starting with but excluding the 'V', contained within the last brackets only?
即-1.1、2.3、4
i.e. - 1.1, 2.3, 4
推荐答案
尝试此UDF
Function ExtractByRegex(sTxt As String)
With CreateObject("VBScript.RegExp")
.Pattern = "V\d+(.)?(\d+)?"
If .Test(sTxt) Then ExtractByRegex = .Execute(sTxt)(0).Value
End With
End Function
更新
这是另一个版本,您可以在其中格式化输出
Update
Here's another version in which you can format the output
Sub Test_ExtractByRegex_UDF()
MsgBox ExtractByRegex("A9 V2.3 8.99")
End Sub
Function ExtractByRegex(sTxt As String)
With CreateObject("VBScript.RegExp")
.Pattern = "V\d+(.)?(\d+)?"
If .Test(sTxt) Then sTxt = Replace(.Execute(sTxt)(0).Value, "V", "")
If Not InStr(sTxt, ".") Then sTxt = sTxt & "." & "000"
ExtractByRegex = Split(sTxt, ".")(0) & IIf(InStr(sTxt, "."), ".", "") & Format(Split(sTxt, ".")(1), "000")
End With
End Function
这篇关于从Excel VBA中的字符串中提取最后一个括号字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文