Regex - match anything but a string

Regular expressions are something of a black art for programmers, and many get by without ever really learning them properly. Sure, most people will be able to use them to some level, but if you don't use them for complex tasks in your day to day work you'll never really need their advanced functionality. I sadly fell into this camp the other day when I needed to say 'match everything except a whole word.' This sounds easy doesn't it, but actually it's less obvious than it seems...

If I want to exclude an individual letter or number it's an easy task, let's say I wanted to match everything from the start of a string up until either a 'z' or a '7':

$text[] = 'The quick brown fox jumped over the lazy dog';
$text[] = '0 seven 3 four 9 six 7 four 2';
 
foreach($text as $value)
{
	echo '<p>Considering: '.$value.'<br />';
	if(preg_match('~^([^z7]*)~s', $value, $matches))
	{
		echo 'Found: '.$matches[1];
	}
	echo '</p>';
}

Produces:

Considering: The quick brown fox jumped over the lazy dog
Found: The quick brown fox jumped over the la

Considering: 0 seven 3 four 9 six 7 four 2
Found: 0 seven 3 four 9 six

Now what if I wanted to do the same for the word 'lazy' or 'six' in the same phrases? It's not hard either, but it's not obvious:

$text[] = 'The quick brown fox jumped over the lazy dog';
$text[] = '0 seven 3 four 9 six 7 four 2';
 
foreach($text as $value)
{
	echo '<p>Considering: '.$value.'<br />';
	if(preg_match('~^((?:(?!lazy|six).)*)~s', $value, $matches))
	{
		echo 'Found: '.$matches[1];
	}
	echo '</p>';
}

Produces:

Considering: The quick brown fox jumped over the lazy dog
Found: The quick brown fox jumped over the

Considering: 0 seven 3 four 9 six 7 four 2
Found: 0 seven 3 four 9

This uses something called a negative lookahead (?!) preceeding the dot (any character) which I've told not to be included in a match (?:). Simples.

Hope this saves someone the time it took me to read up on!