473,405 Members | 2,334 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,405 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 8466
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.