Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - strange behavior from Regex.Split & myString.IndexOf

Reply
 
Old 04-01-2008   #1 (permalink)
mad.scientist.jr


 
 

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

My System SpecsSystem Spec
Old 04-01-2008   #2 (permalink)
Peter Duniho


 
 

Re: strange behavior from Regex.Split & myString.IndexOf

On Tue, 01 Apr 2008 17:55:16 -0700, <mad.scientist.jr@xxxxxx> wrote:
Quote:

> 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.
On the latter point, I don't understand what you mean. In the code you
posted, IndexOf() is going to be 0 if the string actually passes the test
that StartsWith() applies. But you are checking for "> 0". The two lines
you wrote are definitely not logically equivalent.

As far as Regex.Split() goes, surely this has to do with the fact that the
"$" character is a special regular expression character. If you don't
want to apply an actual regular expression to your string processing,
don't use Regex. That's where the name "Regex" comes from: "REGular
EXpression".

Use String.Split() instead.

Pete
My System SpecsSystem Spec
Old 04-01-2008   #3 (permalink)
mad.scientist.jr


 
 

Re: strange behavior from Regex.Split & myString.IndexOf

Aha... string.IndexOf is 0 based. Silly mistake on my part. Duh

I can think of other places where I would want to use Regex.split
because it lets you split by more than one character. So you would
need to escape any delimiter characters that would be interpreted as a
regular expression? Did not know that.

Thanks

On Apr 1, 9:02 pm, "Peter Duniho" <NpOeStPe...@xxxxxx>
wrote:
Quote:

> On Tue, 01 Apr 2008 17:55:16 -0700, <mad.scientist...@xxxxxx> wrote:
Quote:

> > 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.
>
> On the latter point, I don't understand what you mean. In the code you
> posted, IndexOf() is going to be 0 if the string actually passes the test
> that StartsWith() applies. But you are checking for "> 0". The two lines
> you wrote are definitely not logically equivalent.
>
> As far as Regex.Split() goes, surely this has to do with the fact that the
> "$" character is a special regular expression character. If you don't
> want to apply an actual regular expression to your string processing,
> don't use Regex. That's where the name "Regex" comes from: "REGular
> EXpression".
>
> Use String.Split() instead.
>
> Pete
My System SpecsSystem Spec
Old 04-01-2008   #4 (permalink)
Peter Duniho


 
 

Re: strange behavior from Regex.Split & myString.IndexOf

On Tue, 01 Apr 2008 18:17:47 -0700, <mad.scientist.jr@xxxxxx> wrote:
Quote:

> Aha... string.IndexOf is 0 based. Silly mistake on my part. Duh
>
> I can think of other places where I would want to use Regex.split
> because it lets you split by more than one character.
String.Split() does too. Just pass it strings instead of characters.
Quote:

> So you would
> need to escape any delimiter characters that would be interpreted as a
> regular expression? Did not know that.
Yes...if you want characters that would ordinarily be interpreted as a
control character for the regular expression, it needs to be escaped.
Just as is the case any time you might have special characters embedded in
a string (like quote, newline, etc. for plain old string literals).

Pete
My System SpecsSystem Spec
Old 04-04-2008   #5 (permalink)
Jesse Houwing


 
 

Re: strange behavior from Regex.Split & myString.IndexOf

Hello mad.scientist.jr@xxxxxx,

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
Quote:

> 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


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: CSV and regex s.split(",") and empty fields VB Script
Re: how to with regex split into tokens PowerShell
[Regex]'s split method PowerShell
[regex]::Split help PowerShell
Where are things like -replace and split([regex} documented? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46