473,326 Members | 2,255 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,326 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 1879
{ 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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.