SimonDau
SimonDau

Reputation: 547

Match date and time from string with specific conditions

The problem: Have a long string of text and need to extract the date out of it. However, the string has multiple dates so certain conditions should be met to succeed.

The strings with date fragments within the text are as follows:

of Los Angeles 12/19/2018 11:39 AM She
as a Unit 1/18/2019 8:30 AM 4 1.

Using the regex below I can match two dates:

<cfset dates = reMatch("(\d+/\d+/\d+)([^\s]([ ]{1,}))((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))(?=([ ]{1,}))(?=[0-9])", text)>

Matched dates:
12/19/2018 11:39 AM
1/18/2019 8:30 AM

I'm trying to match the date and time followed by one or more spaces and one or more digits (the second instance only), which would discard the first instance and leave me in a happy place. So far I've tried the following code but it discards both dates, so there something not quite right with the last blocks of regex:

<cfset dates = reMatch("(\d+/\d+/\d+)([^\s]([ ]{1,}))((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))(?=([ ]{1,}))(?=[0-9])", text)>

Any help would be highly appreciated!

Upvotes: 1

Views: 127

Answers (1)

The fourth bird
The fourth bird

Reputation: 163467

You have this part at the end of the pattern (?=([ ]{1,}))(?=[0-9]) which are 2 separate assertions, where the first asserts 1 or more spaces, and the second asserts a digit directly to the right directly from the current position.

That will never be true.

What you can do is use a single assertion (?= +\d), and if you don't need all the capture groups, omit them to get a match with a lookahead only.

Using a case insensitive match (as the only chars are a p and m and currently can be matched in any combination)

\b\d+/\d+/\d+ +(?:1[0-2]|0?[1-9]):[0-5][0-9] ?[ap]m(?= +\d)

Regex demo

Note that this is a broad match \d+/\d+/\d+ which you might also refine using a pattern like showed at this page.

The version with the capture groups:

(\d+/\d+/\d+) +((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))(?= +\d)

Regex demo

Upvotes: 2

Related Questions