Excel·VBA日期时间转换提取正则表达式函数

标准日期转换

vbnet 复制代码
Function 标准日期(ByVal str$) As Date
    Dim pat$, result$
    arr = Array("(\d{4}).*?(\d{1,2}).*?(\d{1,2})", "(\d{4}).*?(\d{1}).*?(\d{1,2})")
    If Len(str) < 8 Then pat = arr(1) Else pat = arr(0)
    With CreateObject("vbscript.regexp")  '正则表达式
        .Global = True
        .Pattern = pat
        result = .Replace(str, "$1/$2/$3")
    End With
    标准日期 = Format(result, "yyyy/mm/dd")
End Function

标准日期时间提取

vbnet 复制代码
Function 标准日期时间(ByVal str$) As Date
    Dim pat$, res$, result$, t&
    On Error Resume Next
    pat = "(\d{4}).*?(\d{1,2}).*?(\d{1,2}).*?(\d{1,2}).*?(\d{1,2}).*?(\d{1,2}).*"
    res = "$1/$2/$3 $4:$5:$6"
    With CreateObject("vbscript.regexp")  '正则表达式
        .Global = True
        .Pattern = pat
        Do
            result = .Replace(str, res): t = CLng(Mid(result, 1, 4))
            If CDate(result) = "0:00:00" Or t < 1900 Then str = Mid(str, 2) Else Exit Do
            If Len(str) < 6 Then result = "#Error": Exit Do  '没有日期
        Loop While Len(str) > 1
    End With
    标准日期时间 = Format(result, "yyyy/mm/dd hh:mm:ss")
End Function
相关推荐
htj101 小时前
C# 使用正则表达式
正则表达式·c#
ZZZKKKRTSAE2 小时前
快速上手Linux全局搜索正则表达式(grep)
linux·服务器·正则表达式
沉到海底去吧Go13 小时前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格
Kusunoki_D18 小时前
Python-正则表达式(re 模块)
python·正则表达式
数字芯片实验室19 小时前
正则表达式的前世今生
正则表达式
Abigail_chow1 天前
EXCEL如何快速批量给两字姓名中间加空格
windows·microsoft·excel·学习方法·政务
Lenyiin2 天前
《 C++ 点滴漫谈: 四十 》文本的艺术:C++ 正则表达式的高效应用之道
c++·正则表达式·lenyiin
AA-代码批发V哥2 天前
Java正则表达式完全指南
java·正则表达式
xiaohezi2 天前
Rag chunk 之:Excel 文档解析
excel
weixin_472339462 天前
python批量解析提取word内容到excel
python·word·excel