加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – VBA正则表达式匹配时间范围,如“下午1:30 – 凌晨

发布时间:2020-12-13 22:54:00 所属栏目:百科 来源:网络整理
导读:我正在尝试使用VBA正则表达式来验证表单的时间范围:#0:00xm – #0:00xm其中x是a或p.所以字符串文字可能是“下午1:30 – 凌晨12:00”.我想匹配具有此模式的单元格. 当我在这个在线工具中使用常规表达时:http://public.kvalley.com/regex/regex.asp并检查
我正在尝试使用VBA正则表达式来验证表单的时间范围:#0:00xm – #0:00xm其中x是a或p.所以字符串文字可能是“下午1:30 – 凌晨12:00”.我想匹配具有此模式的单元格.

当我在这个在线工具中使用常规表达时: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)
    End If
Next rCell
对于任何关心的人来说,这是我固定的工作版本,特别感谢dda更简单的RegEx ^^:
Dim rRange As Range
Dim rCell As Range

Dim re As Object
Set re = CreateObject("vbscript.regexp")
With re
  .Pattern = "^dd?:dd[aApP][mM] - dd?:dd[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,0)
    Else
        rCell.Interior.Color = RGB(250,0)
    End If
Next rCell

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读