472,111 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

strange behavior from Regex.Split & myString.IndexOf

I am working in C# ASP.NET framework 1.1 and
for some reason Regex.Split isn't working as expected.
When trying to split a string, Split is returning an array
with the entire string in element [0] and an empty string in element
[1].
I am trying two different ways (an ArrayList and a string array)
and both are doing that. Also, IndexOf is not working,
but StartsWith does.

The code:

using System.Text.RegularExpressions;
...
// some code to find CheckBoxList controls in
Request.Form
// look at Request.Form keys and split out delimiter
// which sometimes is "$" (ie "CheckBoxList1$1")
// and sometimes ":" (ie CheckBoxList1:1)
// (is there any property for what .NET uses?), so to
get delim we
// use a function to return 1st non alphanum char
after control name
string _sIndexDelim =
GetControlNameDelimiter(CheckBoxList1.ID);
foreach (string _key in
HttpContext.Current.Request.Form.AllKeys)
{
//FOR SOME ODD REASON, THIS DOESN'T WORK:
//if (_key.IndexOf(CheckBoxList1.ID +
_sIndexDelim, 1) 0)
// BUT THIS WORKS (?)
if (_key.StartsWith(CheckBoxList1.ID +
_sIndexDelim))
{
_arrKey = new ArrayList();
_arrKey.AddRange(Regex.Split(_key,
_sIndexDelim)); // does not split!
string[] substrings = Regex.Split(_key,
_sIndexDelim); // does not split!

The values:

?_key
"CheckBoxList1$1"
?_sIndexDelim
"$"

?_arrKey[0]
"CheckBoxList1$1"
?_arrKey[1]
""

?substrings[0]
"CheckBoxList1$1"
?substrings[1]
""

Any ideas why this is happening?

Thanks
Apr 2 '08 #1
1 3181
Hello ma**************@gmail.com,

This isn't working because $ has a special meaning in Regular expressions
(end of line, or end of input).

To make sure the regex works with any input use string regex = Regex.Escape("$");

which should make it \$.
Jesse
I am working in C# ASP.NET framework 1.1 and
for some reason Regex.Split isn't working as expected.
When trying to split a string, Split is returning an array
with the entire string in element [0] and an empty string in element
[1].
I am trying two different ways (an ArrayList and a string array)
and both are doing that. Also, IndexOf is not working,
but StartsWith does.
The code:

using System.Text.RegularExpressions;
...
// some code to find CheckBoxList controls in
Request.Form
// look at Request.Form keys and split out delimiter
// which sometimes is "$" (ie "CheckBoxList1$1")
// and sometimes ":" (ie CheckBoxList1:1)
// (is there any property for what .NET uses?), so to
get delim we
// use a function to return 1st non alphanum char
after control name
string _sIndexDelim =
GetControlNameDelimiter(CheckBoxList1.ID);
foreach (string _key in
HttpContext.Current.Request.Form.AllKeys)
{
//FOR SOME ODD REASON, THIS DOESN'T WORK:
//if (_key.IndexOf(CheckBoxList1.ID +
_sIndexDelim, 1) 0)
// BUT THIS WORKS (?)
if (_key.StartsWith(CheckBoxList1.ID +
_sIndexDelim))
{
_arrKey = new ArrayList();
_arrKey.AddRange(Regex.Split(_key,
_sIndexDelim)); // does not split!
string[] substrings = Regex.Split(_key,
_sIndexDelim); // does not split!
The values:

?_key
"CheckBoxList1$1"
?_sIndexDelim
"$"
?_arrKey[0]
"CheckBoxList1$1"
?_arrKey[1]
""
?substrings[0]
"CheckBoxList1$1"
?substrings[1]
""
Any ideas why this is happening?

Thanks
--
Jesse Houwing
jesse.houwing at sogeti.nl
Apr 4 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Stu Cazzo | last post: by
4 posts views Thread by H | last post: by
3 posts views Thread by DevBoy | last post: by
7 posts views Thread by bill tie | last post: by
4 posts views Thread by dchan1 | last post: by
17 posts views Thread by clintonG | last post: by
1 post views Thread by mad.scientist.jr | last post: by
reply views Thread by leo001 | last post: by

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.