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];
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/gat the end of your regex and it worked as expected. What are you using that for?