Hi all.

I have managed over the last few years to get by with relativly little regex
knowledge.

However I now have what seems like a simple problem which I simply cannot
find a solution for.

As stated Regular expressions are not my strong point.

What I need is a way to replace a set string within a source but only where
that set string is not surounded by brackets.

Thus I would like to (for instance) change "AB(A)BABA" into "CB(A)BCBC"

Must I match all the A's and then loop to find those that are not surrounded
or is there a better way?

Thanks in advance

RE: Regex replace where Search Value not between specific delimiters by wawang

wawang
Fri Jun 08 02:17:21 CDT 2007

Hi Rory,

You can achieve this with "Negative Lookahead":

Regex RegexObj = new Regex("(?!\\()A(?!\\))");
Debug.Assert(RegexObj.Replace("AB(A)BABA", "C")=="CB(A)BCBC");


<quote>
#Grouping Constructs
http://msdn2.microsoft.com/en-us/library/bs2twtah.aspx

(?! subexpression)

(Zero-width negative lookahead assertion.) Continues match only if the
subexpression does not match at this position on the right. For example,
\b(?!un)\w+\b matches words that do not begin with un.
</quote>


Hope this helps.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.