Solved

Regular Expressions

Posted on 2006-11-08
5
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 63

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

752 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