Take the 2-minute tour ×

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.

share|improve this question
Why do you need to "find" the dash? Is the character at the dash position going to vary? Or do you just want to check that the dash is properly placed? –  Jon Gauthier Dec 14 '12 at 23:27
What language are you using? –  Asad Dec 14 '12 at 23:27
Can you give us a piece of real sample data, so we have proper input data against which to test our solutions? –  ghoti Dec 14 '12 at 23:37
The dash will eventually be replaced but this is just to see that we can find the pattern and generally learning for me. @ Asad this is a pcre_regex library it'll be used by various languages. @ghoti "This is a test 12:45 PM - 1:40 AM this is a test" –  user1905381 Dec 15 '12 at 0:15
add comment

3 Answers

up vote 1 down vote accepted

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 - following AM:

(?<=AM )-

Depending on the likely-hood of false positives in your data this might need to be tighten up, such as HAM - CHEESE will also match, so using positive look-behind and look-ahead:

(?<=:\d{2} AM )-(?= \d{1,2}:\d{2} PM)

?<=      # Positive look-behind 
:        # Match colon
\d{2}    # Followed by 2 digits (and a space)
AM       # Followed by AM (and a space)
-        # Match hyphen if look-behind is met
?=       # Positive look-ahead
 \d{1,2} # Match either 1 or 2 digits
:        # Followed by a colon 
\d       # Followed by 2 more digits 
 PM      # Finally a space and PM   

That should rule out any false positives.

Demo with grep:

$ echo '(any text) XX:XX AM - XX:XX PM (any text)' | grep -Po '(?<=AM )-'

$ echo '12:45 AM - 1:20 PM' | grep -Po '(?<=:\d{2} AM )-(?= \d{1,2}:\d{2} PM)'

Another option is using capture groups, the following regexp will match the whole line and the - will be matched in capture group 1:

share|improve this answer
Very complete. Ty. –  user1905381 Dec 15 '12 at 0:17
add comment

This will find the dash:


This uses look arounds, so the whole regex matches just the connecting character

share|improve this answer
add comment
/\d\d:\d\d [ap]m (.) \d\d:\d\d [ap]m/i
share|improve this answer
add comment

Your Answer


