VBA Regex - Grab Hour HH:MM from String

Jebathon

Given a arbitary string I want to grab an hour (HH:MM) from the string.

Here is my regex:

^                   # Start of string
(?:                 # Try to match...
 (?:                #  Try to match...
  ([01]?\d|2[0-3]): #   HH:
 )?                 #  (optionally).
 ([0-5]?\d):        #  MM: (required)
)?                  # (entire group optional, so either HH:MM:, MM: or nothing)
$                   # End of string

And my code:

Public Sub RegexTest()

 Dim oRegex As Object
 Dim time_match As Object

 Set oRegex = CreateObject("vbscript.regexp")
 With oRegex
  .Global = True
 .Pattern = "^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)$" 'HH:MM
 End With


 Dim s As String: s = "START TIME: Mar. 3rd 2016 12:00am"

 Set time_match = oRegex.Execute(s)
 If time_match.Count = 1 Then
   Debug.Print time_match.Matches(0)
 Else

 End If


End Sub

However I am unable to match here and get no output.

Wiktor Stribiżew

Your ^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)$ pattern only matches a full string that starts with an optional HH: part, and and obligatory MM part followed with an obligatory :.

I suggest

(?:[01]?\d|2[0-3]):[0-5]\d

Since you are matching a part of the string.

See regex demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Obtain Java hour from String of type "hh:mm:ss"

From Dev

How to get hour min and am/pm from string that contains HH:MM AM/PM?

From Dev

Regex for hour, minute and second format HH:MM:SS

From Dev

Javascript RegEx to MASK 24 hour and minute (hh:mm)

From Dev

Regex for hour, minute and second format HH:MM:SS

From Dev

Javascript RegEx to MASK 24 hour and minute (hh:mm)

From Dev

Regex to ensure input starts with +/- then number, or is an hour in hh:mm format

From Dev

Fastest way to extract hour from time (HH:MM)

From Dev

extract NSTimeZone from string like '+-hh:mm'

From Dev

regex to grab from char/string to another

From Dev

Regex to grab the data from an address string

From Dev

Regex pattern for EXACTLY HH:MM:SS time String

From Dev

regex for a timestamp HH:MM:SS?

From Dev

Calculate total seconds from string in format "HH:mm:ss,fff"

From Dev

Stripping time (in the format hh:mm) from a string in Python 2

From Dev

Converting from String to specific HH:mm date format

From Dev

How to get local time from a specific string adding hh:mm

From Dev

Convert date from string "yyyyddmm hh:mm:ss" to String "DD/MM/YYYY" in JAVA

From Dev

Convert String to hh:mm format

From Dev

Format String to HH:mm in Swift

From Dev

Add time to HH:mm string

From Dev

Regex from a html parsing, how do I grab a specific string?

From Dev

RegEx matching HH:MM, HH:MM:SS OR HH:MM:SS.XXX

From Dev

Hour difference between two times(HH:MM:SS a)in momentjs

From Dev

How to addition of multiple hour in php in HH:MM format

From Dev

Wrong hour difference between 2 timestamps (hh:mm:ss)

From Dev

0-23 hour military clock to standard time (hh:mm)

From Dev

How to convert decimal hour value to hh:mm:ss

From Dev

calculate average of time(hh:mm:ss) of a 24 hour clock

Related Related

  1. 1

    Obtain Java hour from String of type "hh:mm:ss"

  2. 2

    How to get hour min and am/pm from string that contains HH:MM AM/PM?

  3. 3

    Regex for hour, minute and second format HH:MM:SS

  4. 4

    Javascript RegEx to MASK 24 hour and minute (hh:mm)

  5. 5

    Regex for hour, minute and second format HH:MM:SS

  6. 6

    Javascript RegEx to MASK 24 hour and minute (hh:mm)

  7. 7

    Regex to ensure input starts with +/- then number, or is an hour in hh:mm format

  8. 8

    Fastest way to extract hour from time (HH:MM)

  9. 9

    extract NSTimeZone from string like '+-hh:mm'

  10. 10

    regex to grab from char/string to another

  11. 11

    Regex to grab the data from an address string

  12. 12

    Regex pattern for EXACTLY HH:MM:SS time String

  13. 13

    regex for a timestamp HH:MM:SS?

  14. 14

    Calculate total seconds from string in format "HH:mm:ss,fff"

  15. 15

    Stripping time (in the format hh:mm) from a string in Python 2

  16. 16

    Converting from String to specific HH:mm date format

  17. 17

    How to get local time from a specific string adding hh:mm

  18. 18

    Convert date from string "yyyyddmm hh:mm:ss" to String "DD/MM/YYYY" in JAVA

  19. 19

    Convert String to hh:mm format

  20. 20

    Format String to HH:mm in Swift

  21. 21

    Add time to HH:mm string

  22. 22

    Regex from a html parsing, how do I grab a specific string?

  23. 23

    RegEx matching HH:MM, HH:MM:SS OR HH:MM:SS.XXX

  24. 24

    Hour difference between two times(HH:MM:SS a)in momentjs

  25. 25

    How to addition of multiple hour in php in HH:MM format

  26. 26

    Wrong hour difference between 2 timestamps (hh:mm:ss)

  27. 27

    0-23 hour military clock to standard time (hh:mm)

  28. 28

    How to convert decimal hour value to hh:mm:ss

  29. 29

    calculate average of time(hh:mm:ss) of a 24 hour clock

HotTag

Archive