473,394 Members | 1,740 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,394 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 3254
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
4
by: H | last post by:
This is kind of an followup on oneof my previous questions, and it has with RegEx to do. I have a string containing of several words. What would a good regex expression looklike to get one match...
3
by: DevBoy | last post by:
I am in need of parsing string based on characters like / or { or } or ^ However, anytime I try and run the following code I do not the proper results (It always returns the same string unparsed....
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
4
by: dchan1 | last post by:
String line = " I am new to c# "; How to use Regex to split the string so that It would return a String array with: token = "I" token = "am" token = "new" token =...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
3
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}")...
1
by: mad.scientist.jr | last post by:
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 ...
14
by: Stevo | last post by:
If you split a string into an array using the split method, it's not working the way I'd expect it to. That doesn't mean it's wrong of course, but would anyone else agree it's working somewhat...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.