473,836 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strange behavior from Regex.Split & myString.IndexO f

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.Reg ularExpressions ;
...
// 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 =
GetControlNameD elimiter(CheckB oxList1.ID);
foreach (string _key in
HttpContext.Cur rent.Request.Fo rm.AllKeys)
{
//FOR SOME ODD REASON, THIS DOESN'T WORK:
//if (_key.IndexOf(C heckBoxList1.ID +
_sIndexDelim, 1) 0)
// BUT THIS WORKS (?)
if (_key.StartsWit h(CheckBoxList1 .ID +
_sIndexDelim))
{
_arrKey = new ArrayList();
_arrKey.AddRang e(Regex.Split(_ key,
_sIndexDelim)); // does not split!
string[] substrings = Regex.Split(_ke y,
_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 3296
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.Reg ularExpressions ;
...
// 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 =
GetControlNameD elimiter(CheckB oxList1.ID);
foreach (string _key in
HttpContext.Cur rent.Request.Fo rm.AllKeys)
{
//FOR SOME ODD REASON, THIS DOESN'T WORK:
//if (_key.IndexOf(C heckBoxList1.ID +
_sIndexDelim, 1) 0)
// BUT THIS WORKS (?)
if (_key.StartsWit h(CheckBoxList1 .ID +
_sIndexDelim))
{
_arrKey = new ArrayList();
_arrKey.AddRang e(Regex.Split(_ key,
_sIndexDelim)); // does not split!
string[] substrings = Regex.Split(_ke y,
_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
31187
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 will split myString up using the delimiter of 1 space so that
4
1521
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 on every word ? For example : String myString =" This is the string that stupid H can't split up"; // A good RegEx needed here .. So the result would look something like this ;
3
265
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. Regex rx = new Regex("{"); string segmentData = rx.split(str); Any helpful hints?
7
2623
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) every occurrence of characters "p", "q", "r", "s" with "y". Right now, I do it as follows:
4
3390
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 = "to" token = "c#"
17
3984
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 http://forta.com/books/0672325667/
3
8263
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;}") Thanks, Craig
1
1925
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 and an empty string in element . 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:
14
1738
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 illogically? Here's a test I just put together that splits on "&". The test strings are: "a&b" = (Correct!) I expect array length 2 and I get 2 "a&" = (Incorrect!) I expect array length 1 but I get 2 "a" = (Correct!) I expect array length 1 and...
0
9825
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9672
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10854
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10601
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9388
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5652
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5829
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4460
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.