I am having trouble getting a very simple .NET Regex.Replace to work properly. Basically, I have a set of data containing names and I want to make sure that each name is unique before saving it to the underlying Active Directory. For example, say I have 5 identically named objects called "mycontact". I have a saving routine that attempts to save it using the specified name, but if the name is already in use, it will throw an exception so that I can increment a value and append it to the end of the name, ensuring uniqueness. See code below.
Instead of getting a set of objects in the underlying AD like this:
I get this:
What am I doing wrong?
Dim name As String = "mycontact"
Dim Increment As Integer = 1
Dim SaveSuccessful As Boolean = False
While Not SaveSuccessful
SaveSuccessful = True
Catch ex As ObjectAlreadyExistsException
name = Regex.Replace(name, "(" & Increment.ToString() & ")?$", (Increment + 1).ToString())
Increment += 1