471,594 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

Query String or Connection String with Regex

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

}

Jan 10 '07 #1
3 8158
jw********@gmail.com wrote:
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

Jan 10 '07 #2
Wow, Split() is significantly faster. Thank you!

J Wolfgang Goerlich

Jan 10 '07 #3
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
--
http://www.sturmnet.org/blog
Jan 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.