Search code examples
regexcharacterregular-language

Regex n character to n character


7/25/2015 5:45:40 AM ... I am trying to return "5:45:40 AM" the time from this string using regex, I just learned about this abstract language 15 minutes ago. I am having a really difficult time. I know how to get to the beginning of the first to nth characters and last nth characters but, not n to n characters. Is that even possible? You would think its something like .({11}, .{20}$) Thank you.


Solution

  • Going by character count would be incorrect in this situation, because the length of the date portion is variable: 8/1/2015 is only 8 characters long, while 12/12/2015 is ten characters long.

    You would be better off matching \d{1,2}:\d{2}:\d{2} (AM|PM) to capture the time portion of the string.

    Demo.