Solved

Regular Expressions

Posted on 2006-11-08
5
248 Views
Last Modified: 2010-04-16
I need the exact regular expressions syntax to do the following.  Here is the input statement:

      server=myServer;database=myDatabase;User Id=myuser;pwd=mypass;

I need to make it so that my regex statement will trigger a match for only the "server" and "database" portions of the string above.  In other words, it should return a match AS IF it sees a string like this:

      server=myServer;database=myDatabase;

I found a regex which almost does what I want, this returns a match on each name/value pair for each of the four (4) sets.

      (.+?)(?:=)(.+?)(?:;|$)

I need to modify this so that it only grabs a match for "server" and "database", but I don't see how (or if it's even possible) to match based on groups of words.

0
Comment
Question by:Zoplax
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17902277
(server|database)[^;]*;
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 200 total points
ID: 17902283
For example :

Regex r = new Regex("(server|database)[^;]*;");
MatchCollection matches = r.Matches("server=myServer;database=myDatabase;User Id=myuser;pwd=mypass;");
foreach(Match match in matches){
      Console.WriteLine(match.Value);      
}

outputs :

server=myServer;
database=myDatabase;
0
 
LVL 13

Expert Comment

by:joechina
ID: 17902333
Try this
string s = "server=myServer;User Id=myuser;database=myDatabase;pwd=mypass;";
string result = Regex.Replace(s, @"(?!(server|database)=)\b[^=;]+\b=[^;]+(;|$)","");
0
 
LVL 13

Assisted Solution

by:joechina
joechina earned 50 total points
ID: 17902341
If you want to ignore the case:
string s = "Server=myServer;User Id=myuser;database=myDatabase;pwd=mypass;";
string Result = Regex.Replace(s, @"(?!(server|database)=)\b[^=;]+\b=[^;]+(;|$)","",RegexOptions.IgnoreCase);
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17903680
Hi Zoplax;

This Regex will do what you want and will find them in any order that they may be inputted.

      string input = "server=myServer;database=myDatabase;User Id=myuser;pwd=mypass;";
      string output = string.Empty;
      MatchCollection mc = Regex.Matches(input, @"((?:server|database)\s*=\s*.+?)(?:;|$)",
            RegexOptions.IgnoreCase);
      foreach( Match m in mc )
      {
            output += m.Groups[1].Value + ";";
      }

      // The variable output will contain "server=myServer;database=myDatabase;"
      MessageBox.Show(output);


Fernando
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
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…

747 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

12 Experts available now in Live!

Get 1:1 Help Now