Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regular Expressions

Posted on 2006-11-08
5
Medium Priority
?
253 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 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

Technology Partners: 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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

705 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