Anyone have a Regex pattern for finding name/value pairs in a string?

B

Bill

I have strings returned from a config file that contain zero or more
name/value pairs using a pattern like "name = value;"

I want to extract the value of a given name (passwords, etc.). I want this
to ignore whitespace in the pattern match and not be case sensitive.

Anyone have a pattern that will do this?
 
B

Bill

After some experimentation, I finally wound up with this solution. Although
it works, it does not strike me as elegant and am open to suggestions for
improvement:

public ParseFTPConnectionString(string conn)
{
Regex r = new Regex(@"\s*(\w*)\s*=\s*((\w|\\|\/|\.)*);",
RegexOptions.Compiled);
MatchCollection mc = r.Matches(conn);

foreach (Match m in mc)
{
switch (m.Groups[1].Value.ToLower())
{
case "server":
_Server = m.Groups[2].Value;
break;
case "userid":
_UserID = m.Groups[2].Value;
break;
case "pwd":
_Pwd = m.Groups[2].Value;
break;
case "remotesendpath":
_RemoteSendPath = m.Groups[2].Value;
break;
case "remotercvpath":
_RemoteRcvPath = m.Groups[2].Value;
break;
case "uploadfileprefix":
_UploadFilePrefix = m.Groups[2].Value;
break;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top