2

so this is my first real time using C# and I am having some troubles using regex. So I have a string like this:

string str = "test=CAPTURE1; test2=CAPTURE2; ......."

I am capturing everything between = and ; so:

var matches = Regex.Matches(str, "=([^;]*);/g");

However, I am not able to get the results out of an array:

string str2 = matches[0].Value;

I am not sure what I am doing wrong. Any help is appreciated!

EDIT: So here is what I am trying to implement this to (using @Jason Evans code):

string connection = "Server=localhost;Database=dbTest;User Id=hello;Password=world;";            
var matches = Regex.Matches(connection, "(?<Key>[^=]*)=(?<Value>[^;]*)");

string server = matches[0].Groups["Data"].Value;
string db = matches[1].Groups["Data"].Value;
string un = matches[2].Groups["Data"].Value;
string pw = matches[3].Groups["Data"].Value;           

MsSqlConnectionParameters param = (MsSqlConnectionParameters)e.ConnectionParameters;
param.ServerName = server;
param.DatabaseName = db;
param.UserName = un;
param.Password = pw;

This is still not working for some reason, even though I believe this is right.

EDIT2: What's weird is that this works (using same data)...I'm stumped:

string[] test = { "localhost", "dbTest", "hello", "world" };

MsSqlConnectionParameters param = (MsSqlConnectionParameters)e.ConnectionParameters;

param.ServerName = test[0];
param.DatabaseName = test[1];
param.UserName = test[2];
param.Password = test[3];
4
  • The global flag is implied by the Regex.Matches() method. See here for using global flags though, it's not done like JS: msdn.microsoft.com/en-us/library/b49yw9s8%28v=vs.110%29.aspx Commented Oct 16, 2015 at 20:51
  • I tried you example removing the /g at the end of your regex and it worked as expected. What are you using that for? Commented Oct 16, 2015 at 20:59
  • @Luiso i've update my post so you can see my implementation Commented Oct 16, 2015 at 21:06
  • For more info on getting the results of matches see stackoverflow.com/questions/27444640/… Commented Oct 19, 2015 at 8:03

1 Answer 1

3

Try the following:

namespace ConsoleApplication1
{
    using System.Text.RegularExpressions;

    public class Program
    {
        static void Main(string[] args)
        {
            string str = "test=CAPTURE1; test2=CAPTURE2";

            var matches = Regex.Matches(str, "(?<Key>[^=]*)=(?<Value>[^;]*)");

            string str2 = matches[0].Groups["Key"].Value;
            string str3 = matches[0].Groups["Value"].Value;
        }
    }
}

I use a named capture group (?<Key>) and (?<Data>) to catch the text before and after the '='. That way you can grab the individual part of the string.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I think this is right - but I am still having issues passing the values to what I need. I have updated my post to include my implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.