Link to home
Start Free TrialLog in
Avatar of okwiater
okwiater

asked on

How do I configure .NET regular expression greediness?

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:
mycontact
mycontact2
mycontact3
mycontact4
mycontact5

I get this:
mycontact
mycontact2
mycontact33
mycontact344
mycontact3455

What am I doing wrong?
Dim name As String = "mycontact"
Dim Increment As Integer = 1
Dim SaveSuccessful As Boolean = False
 
While Not SaveSuccessful
    Try
        SaveToUnderlyingDirectory(name)
        SaveSuccessful = True
    Catch ex As ObjectAlreadyExistsException
        name = Regex.Replace(name, "(" & Increment.ToString() & ")?$", (Increment + 1).ToString())
        Increment += 1
    End Try
End While

Open in new window

Avatar of ozo
ozo
Flag of United States of America image

While Not SaveSuccessful
    Try
        SaveToUnderlyingDirectory(name & Increment.ToString() )
        SaveSuccessful = True
    Catch ex As ObjectAlreadyExistsException
        Increment += 1
    End Try
I'm not 100% sure this is right, as I'm not a .NET programmer, but I can see where you're going wrong - give it a try?
Dim name As String = "mycontact"
Dim NameToSave As String = name
Dim Increment As Integer = 1
Dim SaveSuccessful As Boolean = False
 
While Not SaveSuccessful
    Try
        SaveToUnderlyingDirectory(NameToSave)
        SaveSuccessful = True
    Catch ex As ObjectAlreadyExistsException
        NameToSave = Regex.Replace(name, "(" & Increment.ToString() & ")?$", (Increment + 1).ToString())
        Increment += 1
    End Try
End While

Open in new window

Avatar of okwiater
okwiater

ASKER

Thanks for the suggestions; however, I am only interested in a solution that addresses what I am doing wrong with the regular expression.

ozo: Your solution does not utilize a regular expression and as such I would end up with an object called "mycontact1" which does not meet the requirement of the first object being named simply "mycontact" without the "1". I could use an If statement to check for this condition, but it still does not address why this regex is not working.

TerryAtOpus: You say you see where I am going wrong, but you did not make any changes to the regex.

Is there a way to address the problem with my regex without introducing new variables or modifying the logic? Basically, the way my code SHOULD work is like this:

1. The code snippet is called once per object, so with my example of 5 objects, it will be called 5 times.
2. At the start of each of the 5 run-throughs, the name variable is initialized to "mycontact".
3. The first time through my code snippet, the SaveToUnderlyingDirectory method will be called and will be successful. A new object is created called "mycontact" and SaveSuccessful is true.
4. The second time through my code snippet, the SaveToUnderlyingDirectory method is called and throws an ObjectAlreadyExistsException. The regex should replace the Increment (if it is present) with Increment+1. The increment is not present, so name is set to "mycontact2". As you can see from my OP, this run-through works as expected.
5. The third time through my code snippet, the SaveToUnderlyingDirectory method is called once with "mycontact" and again with "mycontact2". Both times an exception is thrown. The regex should again replace the Increment (if it is present, which it is this time) with Increment+1. It seems like this part works, except that it appends the Increment twice. As you can see, this time through results in "mycontact33". So it did successfully increment the 2 to a 3, but it almost seems as if the pattern is matching twice and appending an increment twice. Why might this be?
6. This continues ad infinitum. The fourth time you get "mycontact344" (notice that it replaced the trailing 3 with a 4, and appended a second 4 as well).
7. The fifth time you get "mycontact 3455".

Thoughts?
Regex.Replace(name, "([0-9]*)$", Increment.ToString())
ASKER CERTIFIED SOLUTION
Avatar of okwiater
okwiater

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial