I’m looking to create a regex for these patterns:
[num] [am]
[num] [pm]
[num] [-] [num]
[num] [:] [num]
with or w/o spaces
It’s to determine if there is a time in a sentence. Ideally, it would actually parse out the time. The sentences would be like:
“This event is at 9am at…”
“Blues, 5:30, pressroom..”
“Meet at 5pm!”
“All night 9-8 rockin’ it!”
My project is in .NET in case it’s relevant.
Given your definition, I think something like this is about as good as you can do:
This requires that an hour be between 1-12 and minutes be 0-59. It also requires word boundaries around the time. It doesn’t allow spaces around dashes or colons.
Nevertheless, you are going to get a lot of false positives by definition, especially with your
[num]-[num]format, which looks a lot like dates and other stuff.Tested here. I didn’t find a specific .NET tester, but I think it is all standard.