473,830 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1926
{ 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{wa y{we{wash{our{h air";
Regex rx = new Regex("{");
string[] segmentData = rx.Split(mystri ng);
for ( int ix = 0; ix < segmentData.Len gth; ix++ )
{
Console.WriteLi ne("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******** *****@TK2MSFTNG P12.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(mystri ng);
for ( int ix = 0; ix < segmentData.Len gth; ix++ )
{
Console.WriteLi ne("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************* *@hotTAKETHISOU Tmail.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(tex t, @":-\)");
or without the @ key
// since "\" is a control in the string context you have to pad that too
Regex.Match(tex t, ":-\\)");
Hope that helps,

Du

"DevBoy" <de****@cfl.rr. com> wrote in message
news:e4******** *****@TK2MSFTNG P12.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
8124
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 matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
7
2235
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 _dropContactGarbage As New Regex( _ "(+)|" & _ "(+)|" & _ "(+)|" & _ "(+)|" & _ "(+)|" & _
5
5112
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated by a tool. ' Runtime Version: 1.1.4322.2032 ' ' Changes to this file may cause incorrect behavior and will be lost if ' the code is regenerated. ' </autogenerated>
1
2012
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 starter marker (^). if str is large and does not contain the pattern, line 1 takes much much longer to complete than line 2 and cpu goes nearly full throttle.
1
1556
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 want to lose the data in the files that won't load into an XmlDocument. So I was thinking maybe using a RegEx function, sending a Node Name to the function and having it return the InnerText. Is this a good use for RegEx, or is there a better...
10
3903
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 is using Python's standard re (regular expressions) module to parse the input file. As Python's routines to read from the file and regular expressions are most likely implemented via native libraries I would expect that the C code, which reads...
15
50289
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
1403
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 executing the code string dateTimeTest = "23-12-2006 22:10"; string regexStr = @"^(?=\d)(?:(?!(?:(?:0?|1)(?:\.|-|\/)10(?: \.|-|\/)(?:1582))|(?:(?:0?|1)(?:\.|-|\/)0?9(?:\.|-|\/)(?:
16
2258
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) pattern search functions. Where performance is an issue you can alway write your own specialized routine of course. However, for the occasional pattern search where performance isn't an issue, would most seasoned .NET developers rely on "Regex" and...
6
2078
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 it is called a 100 times but in my real app it could be called 50000 or more times. So my question is if it is somehow possible to speed this up and if so how
0
9642
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
10774
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...
0
10489
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10525
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
10202
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7746
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6950
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5617
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...
2
3959
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.