You better don't even start trying to do this with regular expressions.
You should use a DOM parser for tasks like this. This one for example makes your life really easy.
$html = new simple_html_dom();
$html->load($input);
foreach($html->find('a[class=aaa]') as $link)
$link->href = "http://www.example.com".$link->href;
$result = $html->save();
find
lets you query the DOM very nicely. The parameter is tagtype[attributeName=attributeValue]
where the square brackets are an optional filter. Then you just iterate over every link this function finds, and prepend the href
attribute with your domain.
If you cannot use 3rd-party libraries for some reason, PHP comes with a built-in DOM module. The code will not be quite as short and elegant, but it is still highly preferable to trying to come up with a robust regex.