Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regular Expressions

Posted on 2006-11-08
5
Medium Priority
?
254 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 800 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 200 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 64

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

916 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