How do I prepare a regular expression that avoids partial digits when matched?

I am using pcre_exec()

regular expressions to compile.

I want to match a string with a three-digit number followed by "-", but it shouldn't match if the number is prefixed with one or more digits. eg.

345- => match and matches text => (345-)
:546- = match and matches text => (546-)
 123- = match and matches text => (123-)
2355- = Should not match as 355- is prefixed with "2" digit.

      

I tried ^([\\s|:]*)([0-7]\\d{2})-

it but it doesn't work.

+3


source to share


1 answer


Use look-behind to assert that the previous character is not a digit:



(?<!\d)([0-7]\d{2})-

      

+3


source







All Articles