Regex for getting character within a pattern

2019-06-09 12:34发布

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.

标签: regex replace
3条回答
来,给爷笑一个
2楼-- · 2019-06-09 13:17

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:

^.*\d{1,2}:\d{2}\sAM\s(-)\s\d{1,2}:\d{2}\sPM.*$
查看更多
来,给爷笑一个
3楼-- · 2019-06-09 13:18
/\d\d:\d\d [ap]m (.) \d\d:\d\d [ap]m/i
查看更多
不美不萌又怎样
4楼-- · 2019-06-09 13:35

This will find the dash:

(?i)(?<\d\d?:\d\d?\s*[ap]m\s*).*?(?=\s*\d\d?:\d\d?\s*[ap]m)

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

查看更多
登录 后发表回答