I have a pattern like
(any text) XX:XX AM – XX:XX PM (any text)
where X is a number between 0 and 9 and the number can 1 or two character (ex: 12:45 or 1:20)
I need to find a regex to find the – (dash character) in between that pattern.
I’m new but this was my simple RegEx to find the above pattern:
([\d]{1,2}:[\d]{1,2}|[\d]{1,2}:[\d]{1,2} [aApP][mM])(.*?)([\d]{1,2}:[\d]{1,2}|[\d]{1,2}:[\d]{1,2} [aApP][mM])
This doesn’t get me my ultimate goal of just finding the dash in the middle of the pattern.
Positive look-behind will be the shortest/simplest regexp for this situation but it’s not support everywhere so it will depend on what language/environment you are using.
Match only the
-followingAM:Depending on the likely-hood of false positives in your data this might need to be tighten up, such as
HAM - CHEESEwill also match, so using positive look-behind and look-ahead:That should rule out any false positives.
Demo with
grep:Another option is using capture groups, the following
regexpwill match the whole line and the-will be matched in capture group 1: