Why won’t my regex match the beginning of a string?

I keep coming across this problem when I’m writing regex’s. It doesn’t match if it’s at the beginning of a string.
Using ((?<=[^0-9][1-9]:[0-5][0-9])|(?<=[^0-9]1[0-2]:[0-5][0-9])) ?[AaPp][Mm](?![a-zA-Z])
(meant to match am or pm if it comes after a valid time format)

For this:

12:33am 12:33am 2:23pm 1:00 Am

it matches these:

12:33am 12:33[am] 2:23[pm] 1:00[ Am]

Which is great and all, but it missed the first one.
Is there a general rule when it comes to avoiding problems like this in the future?

Read more here: Source link