gpt4 book ai didi

regex - VBA正则表达式匹配时间范围,如 "1:30pm - 12:00am"

转载 作者:行者123 更新时间:2023-12-02 04:18:11 25 4
gpt4 key购买 nike

我正在尝试使用 VBA 正则表达式来验证以下形式的时间范围:#0:00xm - #0:00xm,其中 x ap。因此字符串文字可能是“1:30pm - 12:00am”。我想匹配具有此模式的单元格。

当我在这个在线工具中使用正则表达式时:http://public.kvalley.com/regex/regex.asp并检查我的表达式,它匹配正确。

但是,当我在 VBA 中使用相同的表达式时,它不匹配。

Dim rRange As Range
Dim rCell As Range

Set rRange = Range("A2", "A4") '"G225")

For Each rCell In rRange.Cells
MsgBox (rCell.Value)
If rCell.Value Like "^([0-9]{1,2}[:][0-9]{2}[apm]{2}[ ][-][ ][0-9]{1,2}[:][0-9]{2}[apm]{2})$" Then
MsgBox ("YES")
'rCell.Interior.Color = RGB(0, 250, 0)
Else
MsgBox ("NO")
'rCell.Interior.Color = RGB(250, 0, 0)
End If
Next rCell

最佳答案

对于任何关心的人,这是我的固定工作版本,特别感谢 dda 的更简单的正则表达式 ^^:

Dim rRange As Range
Dim rCell As Range

Dim re As Object
Set re = CreateObject("vbscript.regexp")
With re
.Pattern = "^\d\d?:\d\d[aApP][mM] - \d\d?:\d\d[aApP][mM]$"
.Global = False
.IgnoreCase = False
End With

Set rRange = Range("A2", "G225")

For Each rCell In rRange.Cells
If re.Test(rCell) Then
rCell.Interior.Color = RGB(0, 250, 0)
Else
rCell.Interior.Color = RGB(250, 0, 0)
End If
Next rCell

关于regex - VBA正则表达式匹配时间范围,如 "1:30pm - 12:00am",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10657878/

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