473,405 Members | 2,310 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.

Regex Issue

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?
Nov 16 '05 #1
5 1886
{ is a special char in regexes - escape it: "\\{" or (preferred) @"{"

DevBoy wrote:
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?

Nov 16 '05 #2
On Thu, 1 Apr 2004 09:54:23 -0500, in msg
<e4*************@TK2MSFTNGP12.phx.gbl>, "DevBoy" <de****@cfl.rr.com>
wrote:
Regex rx = new Regex("{");
string[] segmentData = rx.split(str);


I don't see a problem. I emulated your code and it ran perfectly.
const string mystring = "this{is{the{way{we{wash{our{hair";
Regex rx = new Regex("{");
string[] segmentData = rx.Split(mystring);
for ( int ix = 0; ix < segmentData.Length; ix++ )
{
Console.WriteLine("Segment Data for {0} is {1}", ix,
segmentData[ix]);
}

Segment Data for 0 is this
Segment Data for 1 is is
Segment Data for 2 is the
Segment Data for 3 is way
Segment Data for 4 is we
Segment Data for 5 is wash
Segment Data for 6 is our
Segment Data for 7 is hair

--
John Wood a.k.a Mortimer Schnurd
http://www.loosemarbles.com
Nov 16 '05 #3
I apologize for some confusion. I am having an issue currently with the
following character in regex'ing ^.
"DevBoy" <de****@cfl.rr.com> wrote in message
news:e4*************@TK2MSFTNGP12.phx.gbl...
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?

Nov 16 '05 #4
Regarding <#Q**************@tk2msftngp13.phx.gbl>
DevBoy wrote:
I apologize for some confusion. I am having an issue currently with the
following character in regex'ing ^.

In that case, you do need to escape the characters"

const string mystring = @"this/is{the}way^we/wash{our^hair";

Regex rx = new Regex("/|\\{|\\}|\\^");
string[] segmentData = rx.Split(mystring);
for ( int ix = 0; ix < segmentData.Length; ix++ )
{
Console.WriteLine("Segment Data for {0} is {1}", ix,
segmentData[ix]);
}

Read up on it here: http://tinyurl.com/3g4td
--
John Wood a.k.a. Mortimer Schnurd
mo**************@hotTAKETHISOUTmail.com
Nov 16 '05 #5
These are control chars in Regex: . $ ^ { [ ( | ) ] } * + ? \

so if you want to use them as literal string, you have to append "\" infront
of the char.

Example: text = "hahaha :-)"

and you want to extract the smile ":-)"

// @ tell CSharp to treat everything in the string as a literal string
without any control char
// since ")" is a control char in regex, you have to pad, ")" becomes "\)"
Regex.Match(text, @":-\)");
or without the @ key
// since "\" is a control in the string context you have to pad that too
Regex.Match(text, ":-\\)");
Hope that helps,

Du

"DevBoy" <de****@cfl.rr.com> wrote in message
news:e4*************@TK2MSFTNGP12.phx.gbl...
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?

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
7
by: Mike Labosh | last post by:
I have the following System.Text.RegularExpressions.Regex that is supposed to remove this predefined list of garbage characters from contact names that come in on import files : Dim...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
1
by: rh | last post by:
hi all, take the following 2 c# lines: 1) str = Regex.Replace(str, ".*AAA", ""); 2) str = Regex.Replace(str, "^.*AAA", ""); notice that the only difference is that the pattern in line 2 has a...
1
by: Terry Olsen | last post by:
I download xml logs from several servers every day and read the data out of them using the XmlTextReader. But about 10% of them each day throw exceptions because they are not well formed. I don't...
10
by: igor.kulkin | last post by:
I have a small utility program written in Python which works pretty slow so I've decided to implement it in C. I did some benchmarking of Python's code performance. One of the parts of the program...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
1
by: Brian Christensen | last post by:
Came across the following which puzzled me a bit. I havnt been able to find anyting related to the issue so I hope that one in this group might me be able to clarify things for me: When...
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
6
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...

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.