quantifiers - Regex searching for time doesn't want to be non-greedy -


i'm trying run regex in spiceworks parse through email headers select first instance of time ticket assignment purposes. the regex works, picks instances of time rather one.

here's regex: \.*(0[1]|1[3-7]):\d\d:\d\d

i've tried make in non-greedy doing this: \.*?(0[1]|1[3-7]):\d\d:\d\d doesn't seem work. putting question mark in front of quantifier didn't purpose.

what solution make regex non-greedy or pick first instance?

thanks,

andrew n.

edit: i'm trying achieve regex "13:04:57" instead of whole date.

sample string: received: 127.0.0.1 smtp id co5csp22954317qdb; wed, 6 may 2015 13:02:22 -0700 (pdt) x-received: 127.0.0.1 smtp id j185mr26699743oig.68.1430928141923; wed, 06 may 2015 13:02:21 -0700 (pdt)

you can use lookahead based regex match first instance of time matched regex:

/^(?:(?!(?:0[1]|1[3-7]):\d\d:\d\d).)*((?:0[1]|1[3-7]):\d\d:\d\d)/m 

(?!...) negative lookahead makes sure there no other instance of time before matching time matching first instance.

regex demo


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -