Hello group,
I am working on a query string class. The purpose is to parse
name-value pairs from incoming text. Currently, I am using the Regex
code below. I have two questions.
First, the code below does not work if there is a space in the name.
For example, the text "Initial Catalog=test;" parses to name=Catalog
and value=test.
Second, my primary measure is speed. This has to be very fast code.
Should I use Regex or consider an alternative method?
Thanks in advance,
J Wolfgang Goerlich
The code:
// string Value = incoming query string;
// Remove white space
Value = Value.Trim();
// Append a closing semi-colon, if necessary
if (Value.EndsWith(";") == false)
Value = Value + ";";
// Add a space after each semi-colon
Value = Value.Replace(";", "; ");
// Single-space the query string
while (Value.IndexOf(" ") == -1)
{
Value = Value.Replace(" ", " ");
}
Regex nameval = new Regex(@"(?<name>\S+)\s*=\s*(?<val>[^;]+?)\s*(;|$)",
RegexOptions.Singleline);
foreach (Match m in nameval.Matches(Value))
{
MessageBox.Show("name=[{0}], val=[{1}]" + Environment.NewLine +
"name=|" + m.Groups["name"].ToString() + "|" + Environment.NewLine +
"value=|" + m.Groups["val"].ToString() + "|");
}