ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 667|回复: 9

[分享] 用正则方法分离日期的几种方法小结。

[复制链接]

TA的精华主题

TA的得分主题

发表于 2022-11-15 21:44 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
  1. Sub ll1()
  2.     Dim oRegExp As RegExp  'VB使用正则表达式匹配模式的主要对象
  3.     Dim oMatColl  As MatchCollection '是集合对象,包含有关匹配字符串的信息,该对象包含每个成功匹配的 Match 对象
  4.     Dim oMatch  As match    '成功匹配的对象
  5.     Dim oSubMat As SubMatches  '匹配对象所匹配结果的子项
  6.         Set oRegExp = New RegExp
  7.         
  8.     Dim RegArr(5)
  9.         RegArr(0) = "([\d]{4})/(0[1-9]|[11-12])/(0[1-9]|[11-12])"
  10.         RegArr(1) = "([\d]{2,4})年([\d]{1,2})月([\d]{1,2})日"
  11.         RegArr(2) = "([\d]{4})-(0[1-9]|[11-12])-(0[1-9]|[11-12])"
  12.         RegArr(3) = "([\d]{4}).(0[1-9]|[11-12]).(0[1-9]|[11-12])"
  13.         'REGARR = "([\d]{4})/(0[1-9]|[11-12])/(0[1-9]|[11-12])([\d]{4})年(0[1-9]|[11-12])月(0[1-9]|[11-12])日"
  14.         
  15.    

  16.     Dim Arr(5)
  17.        Arr(0) = "with Regular separate 2022/1/15-Remember"
  18.        Arr(1) = "年月日正则分离2022年5月9日/See Me"
  19.        Arr(2) = "with Regular separate 2022-1-15-68"
  20.        Arr(3) = "with Regular separate 2022.1.15"
  21.        Arr(4) = "with Regular separate 11.15"
  22.        Arr(5) = "with Regular separate 15/5/2022 Return"
  23.       
  24.       
  25.        For ii = 0 To 3
  26.            oRegExp.Pattern = RegArr(ii)
  27.            Set oMatColl = oRegExp.Execute(Arr(ii))
  28.            If oMatColl.Count > 0 Then
  29.                 For jj = 0 To oMatColl.Count - 1
  30.                     Set oMatch = oMatColl.Item(jj)
  31.                     Debug.Print Arr(ii), "-----------", oMatch.Value
  32.                 Next jj
  33.            End If
  34.            
  35.        Next ii
  36.       
  37. End Sub
复制代码

运行结果


with Regular separate 2022/1/15-Remember  -----------   2022/1/1
年月日正则分离2022年5月9日/See Me           -----------   2022年5月9日
with Regular separate 2022-1-15-68        -----------   2022-1-1
with Regular separate 2022.1.15           -----------   2022.1.1


TA的精华主题

TA的得分主题

 楼主| 发表于 2022-11-18 16:19 | 显示全部楼层
  1. Sub ll1()
  2.     Dim oRegExp As RegExp  'VB使用正则表达式匹配模式的主要对象
  3.     Dim oMatColl  As MatchCollection '是集合对象,包含有关匹配字符串的信息,该对象包含每个成功匹配的 Match 对象
  4.     Dim oMatch  As match    '成功匹配的对象
  5.     Dim oSubMat As SubMatches  '匹配对象所匹配结果的子项
  6.         Set oRegExp = New RegExp
  7.         
  8.     Dim RegArr(5), RegStr
  9.         RegArr(0) = "([\d]{4})/(0[1-9]|[11-12])/(0[1-9]|[11-12])"
  10.         RegArr(1) = "([\d]{2,4})年([\d]{1,2})月([\d]{1,2})日"
  11.         RegArr(2) = "([\d]{4})-(0[1-9]|[11-12])-(0[1-9]|[11-12])"
  12.         RegArr(3) = "([\d]{4}).(0[1-9]|[11-12]).(0[1-9]|[11-12])"
  13.         'REGARR = "([\d]{4})/(0[1-9]|[11-12])/(0[1-9]|[11-12])([\d]{4})年(0[1-9]|[11-12])月(0[1-9]|[11-12])日"
  14.         RegStr = "\d{4}(?:年|/|-|.)\d{1,2}月\d{1,2}(?:日|号)"
  15.         RegStr = "\d{4}(?:年|/|-|.)\d{1,2}月\d{1,2}(?:日)"
  16.         RegStr = "\d{4}(?:年)\d{1,2}(?:月)\d{1,2}(?:日)"
  17.         RegStr = "\d{2,4}(?:年|-|/|.)\d{1,2}(?:月|-|/|.)\d{1,2}(日|/||)"
  18.         RegStr = "\d{2,4}(年|-|/|.)\d{1,2}(月|-|/|.)\d{1,2}(日|/||)"
  19.         RegStr = "\d{2,4}(年|-|/|.)\d{1,2}(月|-|/|.)\d{1,4}(日|/||)"
  20.    

  21.     Dim Arr(5)
  22.        Arr(0) = "with Regular separate 2022/1/15-Remember"
  23.        Arr(1) = "年月日正则分离2022年5月9日/See Me"
  24.        Arr(2) = "with Regular separate 2022-1-15-68"
  25.        Arr(3) = "with Regular separate 2022.1.15"
  26.        Arr(5) = "with Regular separate 11.15"
  27.        Arr(4) = "with Regular separate 15/5/2022 Return"
  28.       
  29.       
  30.        For ii = 0 To 5
  31.            oRegExp.Pattern = RegArr(ii)
  32.            oRegExp.Pattern = RegStr
  33.            ''
  34.            Set oMatColl = oRegExp.Execute(Arr(ii))
  35.            ''
  36.            If oMatColl.Count > 0 Then
  37.                 For jj = 0 To oMatColl.Count - 1
  38.                     Set oMatch = oMatColl.Item(jj)
  39.                     Debug.Print Arr(ii), ii, "-----------", oMatch.Value
  40.                 Next jj
  41.            End If
  42.            
  43.        Next ii
  44.       
  45. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2022-12-16 12:36 | 显示全部楼层
IsDate(Str)

0            2022/1/15     True          String        2022/1/15
1            2022年05月09日 True          String        2022/5/9
2            2022-1-15     True          String        2022/1/15
3            2022.1.15     False         String        
5            15/5/2022     True          String        2022/5/15




  1. Sub ll1()
  2.     Dim oRegExp As RegExp  'VB使用正则表达式匹配模式的主要对象
  3.     Dim oMatColl  As MatchCollection '是集合对象,包含有关匹配字符串的信息,该对象包含每个成功匹配的 Match 对象
  4.     Dim oMatch  As Match    '成功匹配的对象
  5.     Dim oSubMat   As SubMatches  '匹配对象所匹配结果的子项
  6.         Set oRegExp = New RegExp
  7.         
  8.     Dim RegArr(5), RegStr
  9.         RegArr(0) = "([\d]{4})/(0[1-9]|[11-12])/(0[1-9]|[11-12])"
  10.         RegArr(1) = "([\d]{2,4})年([\d]{1,2})月([\d]{1,2})日"
  11.         RegArr(2) = "([\d]{4})-(0[1-9]|[11-12])-(0[1-9]|[11-12])"
  12.         RegArr(3) = "([\d]{4}).(0[1-9]|[11-12]).(0[1-9]|[11-12])"
  13.         'REGARR = "([\d]{4})/(0[1-9]|[11-12])/(0[1-9]|[11-12])([\d]{4})年(0[1-9]|[11-12])月(0[1-9]|[11-12])日"
  14.         RegStr = "\d{4}(?:年|/|-|.)\d{1,2}月\d{1,2}(?:日|号)"
  15.         RegStr = "\d{4}(?:年|/|-|.)\d{1,2}月\d{1,2}(?:日)"
  16.         RegStr = "\d{4}(?:年)\d{1,2}(?:月)\d{1,2}(?:日)"
  17.         RegStr = "\d{2,4}(?:年|-|/|.)\d{1,2}(?:月|-|/|.)\d{1,2}(日|/||)"
  18.         RegStr = "\d{2,4}(年|-|/|.)\d{1,2}(月|-|/|.)\d{1,2}(日|/||)"
  19.         RegStr = "\d{2,4}(年|-|/|.)\d{1,2}(月|-|/|.)\d{1,4}(日|/||)"
  20.    
  21. Dim oDate As Date, Str
  22.     oDate = "2022年05月09日"
  23.     ''
  24.     Dim Arr(5)
  25.        Arr(0) = "with Regular separate 2022/1/15-Remember"
  26.        Arr(1) = "年月日正则分离2022年05月09日/See Me"
  27.        Arr(2) = "with Regular separate 2022-1-15-68"
  28.        Arr(3) = "with Regular separate 2022.1.15"
  29.        Arr(5) = "with Regular separate 11.15"
  30.        Arr(4) = "with Regular separate 15/5/2022 Return"
  31.       
  32.       
  33.        For ii = 0 To 5
  34.            oRegExp.Pattern = RegArr(ii)
  35.            oRegExp.Pattern = RegStr
  36.            ''
  37.            Set oMatColl = oRegExp.Execute(Arr(ii))
  38.            ''
  39.            If oMatColl.Count > 0 Then
  40.                 For jj = 0 To oMatColl.Count - 1
  41.                     Set oMatch = oMatColl.Item(jj)
  42.                     Str = oMatch.Value
  43.                     
  44.                     'Debug.Print Arr(ii),
  45.                     Debug.Print ii, oMatch.Value,
  46.                     Debug.Print IsDate(Str), TypeName(Str),
  47.                     If IsDate(Str) Then
  48.                         oDate = oMatch.Value
  49.                         Debug.Print oDate
  50.                     Else
  51.                         Debug.Print
  52.                     End If
  53.                
  54.                 Next jj
  55.            End If
  56.            
  57.        Next ii
  58.       
  59. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2022-12-16 15:33 | 显示全部楼层
再做优化

  1. Sub l()
  2.     Dim Str
  3.     Dim oDate As Date
  4.         Str = "年月日正则分离2022年05月09日/See Me"
  5.         Debug.Print IsDate(DateExp(Str))
  6.         oDate = DateExp(Str)
  7.         Stop
  8. End Sub
  9. Function DateExp(Str)
  10.     Dim oRegExp As RegExp
  11.     Dim oMatColl As MatchCollection
  12.     Dim oMatch As Match
  13.     Dim oSubMat As SubMatches
  14.     Dim RegStr
  15.         Set oRegExp = New RegExp
  16.         RegStr = "\d{2,4}(年|-|/|.)\d{1,2}(月|-|/|.)\d{1,4}(日|/||)"
  17.         oRegExp.Pattern = RegStr
  18.         Set oMatColl = oRegExp.Execute(Str)
  19.         DateExp = oMatColl.Item(0)
  20. End Function
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-5-3 18:05 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
对于这种格式的 2023年05月17日 周三,用left(str,11)跟简单一些。是否好用还需要测试。

  1. Sub del()
  2.    Dim Str, oDate As Date
  3.    Dim Rng As Range
  4.        Set Rng = Selection
  5.        For ii = 1 To Rng.Rows.Count
  6.             Str = Left(Rng(ii, 1), 11)
  7.             oDate = Str
  8.             Debug.Print Str, oDate
  9.             If IsDate(oDate) Then
  10.                 Rng(ii, 1) = oDate
  11.             End If
  12.        Next ii
  13. End Sub
复制代码
image.png

TA的精华主题

TA的得分主题

发表于 2023-6-2 16:32 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
标记学习,谢谢分享代码。

TA的精华主题

TA的得分主题

发表于 2023-6-2 16:45 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-4-14 16:30 | 显示全部楼层
在此学习2022年的帖子, 2022-11-15 21:44对正则确实一点都不懂。





  1. Sub ll2()
  2.       
  3.     Dim oRegExp As RegExp  'VB使用正则表达式匹配模式的主要对象
  4.     Dim oMatColl  As MatchCollection '是集合对象,包含有关匹配字符串的信息,该对象包含每个成功匹配的 Match 对象
  5.     Dim oMatch  As Match    '成功匹配的对象
  6.     Dim oSubMat As SubMatches  '匹配对象所匹配结果的子项
  7.     Dim RegStr, Str
  8.     Dim oDate As Date
  9.         Set oRegExp = New RegExp
  10.         RegStr = "([\d]{2,4})([\d]{1,2})([\d]{1,2})_([\d]{1,2})([\d]{1,2})([\d]{1,2})"
  11.         Str = "C:\隧道南图书馆20240323\IMG_20240323_083145279.jpg "
  12.       
  13.            oRegExp.Pattern = RegStr
  14.            Set oMatColl = oRegExp.Execute(Str)
  15.           Set oMatch = oMatColl.Item(0)
  16.           Debug.Print oMatch.Value
  17.           Debug.Print oMatch.SubMatches.Count
  18.            With oMatch.SubMatches
  19.                Str = .Item(0) & "/" & .Item(1) & "/" & .Item(2)
  20.                Str = Str & " " & .Item(3) & ":" & .Item(4) & ":" & .Item(5)
  21.                oDate = Str
  22.            End With
  23.    
  24.        Debug.Print oDate
  25. End Sub
  26. '''
  27. Sub ll3()
  28.     Dim oRegExp As RegExp  'VB使用正则表达式匹配模式的主要对象
  29.     Dim oMatColl  As MatchCollection '是集合对象,包含有关匹配字符串的信息,该对象包含每个成功匹配的 Match 对象
  30.     Dim oMatch  As Match    '成功匹配的对象
  31.     Dim oSubMat As SubMatches  '匹配对象所匹配结果的子项
  32.     Dim RegStr, Str
  33.     Dim oDate As Date
  34.         Set oRegExp = New RegExp
  35.         RegStr = "([\d]{2,4})-([\d]{1,2})-([\d]{1,2})-([\d]{1,2})-([\d]{1,2})-([\d]{1,2})"
  36.         Str = "C:\隧道南图书馆20240323\Screenshot_2024-03-23-08-43-11-439_com.tencent.map.jpg"
  37.            oRegExp.Pattern = RegStr
  38.            Set oMatColl = oRegExp.Execute(Str)
  39.           Set oMatch = oMatColl.Item(0)
  40.           Debug.Print oMatch.Value
  41.           Debug.Print oMatch.SubMatches.Count
  42.            With oMatch.SubMatches
  43.                Str = .Item(0) & "/" & .Item(1) & "/" & .Item(2)
  44.                Str = Str & " " & .Item(3) & ":" & .Item(4) & ":" & .Item(5)
  45.                oDate = Str
  46.            End With
  47.    
  48.        Debug.Print oDate
  49. End Sub
复制代码


TA的精华主题

TA的得分主题

发表于 2024-4-15 08:44 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
标记,谢谢分享

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-4-18 10:13 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 ning84 于 2024-4-18 10:46 编辑

学习正则方法近十年,还是一知半解。
笨办法学习,一遍一遍做题。


  1. Sub ll()
  2.    Dim Arr(3)
  3.        Arr(0) = "IMG20231121102939.jpg"
  4.        Arr(1) = "IMG_20231121_100743630.jpg"
  5.        Arr(2) = "IMG_20231121_101338692.jpg"
  6.        Arr(3) = ".Screenshot_2024-01-18-06-15-33-976_com.autonavi.minimap.jpg"
  7.    Dim RegArr(2)
  8.        RegArr(0) = "([\d]{2,4})([\d]{1,2})([\d]{1,2})_([\d]{1,2})([\d]{1,2})([\d]{1,2})"
  9.        RegArr(1) = "([\d]{2,4})-([\d]{1,2})-([\d]{1,2})-([\d]{1,2})-([\d]{1,2})-([\d]{1,2})"
  10.        RegArr(2) = "([\d]{2,4})([\d]{1,2})([\d]{1,2})([\d]{1,2})([\d]{1,2})([\d]{1,2})"
  11.    Dim Str
  12.        For ii = 0 To UBound(RegArr)
  13.            For jj = 0 To UBound(Arr)
  14.                Str = RegYYYYMMDDHMS(RegArr(ii), Arr(jj))
  15.                If IsDate(Str) Then
  16.                    Debug.Print Arr(jj), Str
  17.                End If
  18.            Next jj
  19.        Next ii

  20.    
  21.    
  22. End Sub

  23. Function RegYYYYMMDDHMS(RegStr, oStr)
  24.     Dim Str
  25.     Dim oRegExp As RegExp  'VB使用正则表达式匹配模式的主要对象
  26.         Set oRegExp = New RegExp
  27.     Dim oMatColl  As MatchCollection '是集合对象,包含有关匹配字符串的信息,该对象包含每个成功匹配的 Match 对象
  28.     Dim oMatch  As Match    '成功匹配的对象
  29.     'Dim oSubMat As SubMatches  '匹配对象所匹配结果的子项
  30.     Dim oDate As Date
  31.         '''
  32.         oRegExp.Pattern = RegStr
  33.         Set oMatColl = oRegExp.Execute(oStr)
  34.         If oMatColl.Count = 0 Then
  35.             RegYYYYMMDDHMS = oStr
  36.             Exit Function
  37.         End If
  38.         Set oMatch = oMatColl.Item(0)
  39.           With oMatch.SubMatches
  40.                Str = .Item(0) & "/" & .Item(1) & "/" & .Item(2)
  41.                Str = Str & " " & .Item(3) & ":" & .Item(4) & ":" & .Item(5)
  42.           End With
  43.           If IsDate(Str) Then
  44.               oDate = Str
  45.               RegYYYYMMDDHMS = oDate
  46.           End If
  47.         
  48.    
  49. End Function
复制代码



''''

  1. Function RegYYYYMMDDHMS(oStr)
  2.     Dim RegArr(2)
  3.         RegArr(0) = "([\d]{2,4})([\d]{1,2})([\d]{1,2})_([\d]{1,2})([\d]{1,2})([\d]{1,2})"
  4.         RegArr(1) = "([\d]{2,4})-([\d]{1,2})-([\d]{1,2})-([\d]{1,2})-([\d]{1,2})-([\d]{1,2})"
  5.         RegArr(2) = "([\d]{2,4})([\d]{1,2})([\d]{1,2})([\d]{1,2})([\d]{1,2})([\d]{1,2})"
  6.     Dim Str
  7.     Dim oRegExp As RegExp  'VB使用正则表达式匹配模式的主要对象
  8.         Set oRegExp = New RegExp
  9.     Dim oMatColl  As MatchCollection '是集合对象,包含有关匹配字符串的信息,该对象包含每个成功匹配的 Match 对象
  10.     Dim oMatch  As Match    '成功匹配的对象
  11.     'Dim oSubMat As SubMatches  '匹配对象所匹配结果的子项
  12.     Dim oDate As Date
  13.         '''
  14.         For ii = 0 To UBound(RegArr)
  15.             oRegExp.Pattern = RegArr(ii)
  16.             Set oMatColl = oRegExp.Execute(oStr)
  17.             If oMatColl.Count > 0 Then
  18.                 Exit For
  19.             End If
  20.         Next ii
  21.         If oMatColl.Count = 0 Then
  22.             RegYYYYMMDDHMS = oStr
  23.             Exit Function
  24.         End If
  25.         Set oMatch = oMatColl.Item(0)
  26.           With oMatch.SubMatches
  27.                Str = .Item(0) & "/" & .Item(1) & "/" & .Item(2)
  28.                Str = Str & " " & .Item(3) & ":" & .Item(4) & ":" & .Item(5)
  29.           End With
  30.           If IsDate(Str) Then
  31.               oDate = Str
  32.               RegYYYYMMDDHMS = oDate
  33.           End If
  34.         
  35.    
  36. End Function
  37. Sub ll()
  38.     Dim Sht As Worksheet
  39.     Dim Rng As Range
  40.          Set Rng = Selection.CurrentRegion
  41.          Set Sht = Rng.Parent
  42.          With Sht
  43.              Set Rng = .Cells(Rng.Row, "B").Resize(Rng.Rows.Count)
  44.          End With
  45.          'Debug.Print Rng.Address
  46.          
  47.         For ii = 1 To Rng.Rows.Count
  48.              Rng(ii, 4) = RegYYYYMMDDHMS(Rng(ii, 1))
  49.         Next ii
  50.          
  51. End Sub
复制代码

副本.zip

1.28 MB, 下载次数: 10

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-30 16:35 , Processed in 0.042786 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表