Query String or Connection String with Regex

J

jwgoerlich

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() + "|");

}
 
O

Oliver

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() + "|");

}

I would try the split method, should be faster than a regular
expression, as well as work in more cases.

string Value = "Initial Catalog=test;OtherValue=foobar";
string[] KeyValues = Value.Trim().Split(';');
string[] NameValue;
foreach (string NameValuePair in KeyValues) {
NameValue = NameValuePair.Split('=');
MessageBox.Show("name=[{0}], val=[{1}]" + Environment.NewLine +
"name=|" + NameValue[0] + "|" + Environment.NewLine +
"value=|" + NameValue[1] + "|");
}

Depending on your application and the source of the string, I would
suggest investigating HttpUtility class with its URLDecode method.

Oliver
 
O

Oliver Sturm

Hello Oliver,
I would try the split method, should be faster than a regular expression,
as well as work in more cases.

Of course you are right it will be faster - it doesn't have to care for so
many different possibilities, thus has much less overhead. Of course you
can only use String.Split within the narrow boundaries of its
functionality, which is not very flexible, definitely not compared to
Regex.Split. Which leads me to my reason for following up here: what do
you mean by "... as well as work in more cases"?


Oliver Sturm
 

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