I'm writing an auto linking extension for blogengine.net which auto links key phrases in the blog post to specific urls. The issue I'm finding is very often the list of phrases to auto link are sub sets of eachother, eg "bmw" is a subset of "bmw car leasing", so if I use a regex to autolink "bmw car leasing " first then auto link the phrase "bmw" next, the former is already auto linked. The precendence is important, the longer phrases must be auto linked first, then the smaller phrases which may be subsets of the longer ones.
What I need is a regex that will dismiss the match if it's already within an anchor tag, i.e my phrase should be dismissed.
I don't have to use regexes too often , so I'm not totally au fait with them, so far I've managed to put together a regex that will match on anchor tags, but not the reverse which is what I need. eg <a\b[^>]*>stuff(.*?)</a>
Any suggestions and advice would be very welcome.
Addition and hopefully the final solution .... only time will tell:- After a bit of trial and error, the final regex I used is below. This is based on the solution I marked as the answer:-
(?<!<a [^<]+)(?<!<img [^<]+)(?<=[ ,.;!]+)search phrase goes here(?=[ ,.;&!]+)(?!!.*<\\a>)
It allows the text being matched on to be preceded and succeeded by a space and basic punctuation as well as allowing for encoded characters like non breaking spaces
etc. It also avoids anything in an img tag being matched.
I realise it's still not 100% but as far as the requirements go it will suffice.
Thanks to all for your help and input.