what does the regular expression (?<!-) mean

pcre, php, regex

Solution

The `?<!` at the start of a parenthetical group is a negative lookbehind. It asserts that the word `color` (strictly, the `c` in the engine) was not preceded by a `-` character.

So, for a more concrete example, it would match `color` in the strings:

color
+color
someTextColor

But it will fail on something like `-color` or `background-color`. Also note that the engine will not technically "match" whatever precedes the `c`, it simply asserts that it is not a hyphen. This can be an important distinction depending on the context (illustrated on Rubular with a trivial example; note that only the `b` in the last string is matched, not the preceding letter).

Problem

I'm trying to understand a piece of code and came across this regular expression used in PHP's preg_replace function. ``` '/(?<!-)color[^{:]*:[^{#]*$/i' ``` This bit... `(?<!-)` doesnt appear in any of my reg-exp manuals. Anyone know what this means please? (Google doesnt return anything - I dont think symbols work in google.)

Original source