regex - Conditional RgEx with PHP preg_match_all -


just have select conditional regex.

here have problem....

i want select time string 02:00 not quotation marks "02:00" or '02:00'

i using select 02:00 or 2:00 regex

$pattern = '/(\d{2}:\d{2})|(\d{1}:\d{2})/'; preg_match_all($pattern,$content, $matches);  

but selecting "02:00" or '02:00' not sure regex use skip time string.

i got http://www.regular-expressions.info/conditional.html not sure how make regex :(

please me out.

update

thanks lucas trzesniewski , great help.

i resolved issue php code.

 $pattern = '/(?!["\'])\b\d{1,2}:\d{2}\b(?!["\'])/';   preg_match_all($pattern,$content, $matches);  

conditionals won't in case.

the simplest way use lookarounds:

(?<!["'])\b\d{1,2}:\d{2}\b(?!["']) 

demo

  • (?<!["']) (negative lookbehind) make sure preceding character not single or double quote
  • (?!["']) (negative lookahead) same following character

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -