[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I configure .NET regular expression greediness?

Posted on 2009-04-14
5
Medium Priority
?
349 Views
Last Modified: 2013-11-08
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

0
Comment
Question by:okwiater
  • 2
  • 2
5 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 24143341
While Not SaveSuccessful
    Try
        SaveToUnderlyingDirectory(name & Increment.ToString() )
        SaveSuccessful = True
    Catch ex As ObjectAlreadyExistsException
        Increment += 1
    End Try
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 24143349
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

0
 
LVL 1

Author Comment

by:okwiater
ID: 24143517
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?
0
 
LVL 85

Expert Comment

by:ozo
ID: 24143537
Regex.Replace(name, "([0-9]*)$", Increment.ToString())
0
 
LVL 1

Accepted Solution

by:
okwiater earned 0 total points
ID: 24233188
None of the posted solutions are correct. The original regex was:
Regex.Replace(name, "(" & Increment.ToString() & ")?$", (Increment + 1).ToString())

The problem with the regex is that the ? (which matches the preceding expression 0 or 1 times) is actually matching BOTH 0 AND 1 times. So it not only replaces Increment with Increment+1, it also replaces nothing with Increment+1. Hence, the duplicate numbers strung onto the end of the expression.

You could use a look behind to fix the issue, but I opted instead to do away with the regex and go another route.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question