Solved

String (maybe Regular Expression) Help

Posted on 2004-09-30
14
209 Views
Last Modified: 2010-04-15
This should be an easy one, but I don't know regex to save my butt.

I have a string like this (call it string1):
"CN=A1,OU=A2,DC=A3,DC=A4^CN=B1,OU=B2,DC=B3,DC=B4^CN=C1,OU=C2,DC=C3,DC=C4"

From string1 I need this (call it string2):
"A1,B1,C1"

The solution doesn't have to be a regex, but I imagine it would be the most efficient way (and of course I'm looking for the most efficient way).

Please post the exact code (c#) and I'll plug it in.  Thank you!
0
Comment
Question by:sjd0103
  • 6
  • 3
  • 3
  • +2
14 Comments
 
LVL 3

Expert Comment

by:GrumbleBot
ID: 12191099
Well here is one way of doing it. doesn't use Regex though. Not sure if you can do what you want with Regex.

                  string s1 = "CN=A1,OU=A2,DC=A3,DC=A4^CN=B1,OU=B2,DC=B3,DC=B4^CN=C1,OU=C2,DC=C3,DC=C4";
                  string s2 = "";
                  
      int n = 0;
      do
      {
            n = s1.IndexOf("CN=", n, s1.Length-n);
            if(n != -1)
            {
                  s2 = s2 + s1.Substring(n+3, 3);
                  n++;
            }
            else
                  s2 = s2.Remove(s2.Length-1,1);
      }
      while(n != -1);

Hope this helps
Good Luck
0
 

Author Comment

by:sjd0103
ID: 12191373
Thanks, Grumble.  Sorry that I didn't post what I have already (as a benchmark):

string test = "CN=A1,OU=A2,DC=A3,DC=A4^CN=B1,OU=B2,DC=B3,DC=B4^CN=C1,OU=C2,DC=C3,DC=C4";

string[] split = test.Split(char.Parse(","));
string results = string.Empty;

for (int i=0; i<split.Length; i++)
{
      if (split[i].IndexOf("CN=") > -1)
      {
            results += split[i].Remove(0,split[i].LastIndexOf("=")+1) + ",";
      }
}

//After it's done I'll do this:  newVariable = results.TrimEnd(char.Parse(","));

I guess the points will go to whomever can provide more eficient code.  I still think it can be done w/regex...we'll see.  I'm raising the value to add some incentive.
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 250 total points
ID: 12191773
Try this:

StringBuilder results=new StringBuilder();
for (Match match=Regex.Match(test, "(?<=\bCN=)\w+", RegexOptions.ExplicitCapture); match.Success; match=match.NextMatch()) {
     if (results.Length>0)
          results.Append(',');
     results.Append(match.Value);
}
newVariable=results.ToString();
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12191804
(Note: if you want the match to be case-insensitive, e.g. also match "cn=", just add the RegexOptions.CaseInsensitive flag to the regex options)
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12191829
Note that, with regular expressions, the first call will be expensive, but if you do many calls, the already compiled regex will be reused, therefore providing a good performance.
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12200566
On the regex there may be a better way...

Matches matches = Regex.Match(test, "(?<=\bCN=)\w+", RegexOptions.ExplicitCapture);

StringBuilder results=new StringBuilder();
foreach (Match match in matches) {
     if (results.Length>0)
          results.Append(',');
     results.Append(match.Value);
}
newVariable=results.ToString();
0
 

Author Comment

by:sjd0103
ID: 12201286
Quick business trip coming up...I'll award points on Monday.  

BTW, is there no way do do this without a loop?

Thanks!
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 14

Expert Comment

by:AvonWyss
ID: 12204496
NipNFriar_Tuck, you comment is absolutely useless since it's not even compiling, plus if it is fixed it's less performant than my suggestion (more overhead tue to array creation and iterator creation, which is both not needed in my suggestion).

sjd0103, you do need some kind of loop to find all occurences. If you only need one or the first, there is no loop involved in the regex solution.
0
 
LVL 1

Expert Comment

by:gav_jackson
ID: 12215386
dudes, why aren't you using the Replace method of the RegExp object?

string test = "CN=A1,OU=A2,DC=A3,DC=A4^CN=B1,OU=B2,DC=B3,DC=B4^CN=C1,OU=C2,DC=C3,DC=C4";

Console.WriteLine("BEFORE : " + test);
Regex re = new Regex("(\\w{2}=)");
test = test.Replace("^",",");
test = re.Replace(test,"");
Console.WriteLine("AFTER  : " + test);
Console.ReadLine();
0
 
LVL 1

Expert Comment

by:gav_jackson
ID: 12215391
You'll need to namespace to compile the code above

using System.Text.RegularExpressions;
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12215404
Dude, do you think that Replace() is internally anything but a loop? And then having to do two more string replaces is certainly slower then the code I proposed...
0
 
LVL 1

Expert Comment

by:gav_jackson
ID: 12216279
yeah i spose it is...its the way i would have approached this problem, less lines of code to write :)
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12216293
Well, it's not so much less lines, with the additional Replace() calls, is it? ;-)
0
 

Author Comment

by:sjd0103
ID: 12216463
Thanks everyone!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now