473,224 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 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 8446
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Sway | last post by:
I'm writing a web-based "Query analyser" tool for our company intranet. It allows a user to type any SQL statement in a form, and execute it over the Web. The SQL can be a query that returns...
9
by: shank | last post by:
Per a previous suggestion, I'm trying to use a parametized query in Access 2002. The query functions properly in Access. Now I'm trying to call it from ASP. I'm using code I found at...
11
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
2
by: eggie5 | last post by:
I have some code (C#) that runs an SQL update query that sets the value of a column to what the user passes. So, this causes an error when anything the user passes in has a ' character in it. I'm...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.