gpt4 book ai didi

vba - 在 VBA 中检查字符串中的模式

转载 作者:行者123 更新时间:2023-12-01 04:35:06 25 4
gpt4 key购买 nike

我的要求是我需要检查一个字符串变量是否有第一个字符“P”后跟数字。

例如

P0
P123
P22

到目前为止,我已经根据 here 查看了使用 Like 的选项.

最佳答案

你可以使用一个简单的正则表达式:

Function strOut(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "^P\d+$"
strOut = .Test(strIn)
End With
End Function

测试代码

Sub Test()
Debug.Print strOut("P22")
Debug.Print strOut("aP22")
Debug.Print strOut("P12344")
End Sub

关于vba - 在 VBA 中检查字符串中的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36310238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com