473,405 Members | 2,300 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 question (easy?)

sb
Hello,
I have a text file which contains plain text with the normal
carriage-return/linefeed line terminators. With that file I want to find
any occurence of "%R" (case-sensitive) on any line that does _not_ start
with "#"....so that I can replace it with something else later using
Regex.Replace().

An example file would look something like this (the real file is
~100kb..several hundred lines):

--- start of file ---

# description line...if there are any %R's on this line, ignore them\r\n
hey %Rthere\r\n
how's it %Rgoin?\r\n
%Rfine, you?\r\n

--- end of file ---

In the above, the regular expression should match all occurences of %R
except the one on the line that starts with "#". Can someone tell me what
the right regex search string would be? I'm sure this is very easy to do
but I'm still a newbie to regex.

Thanks in advance!
sb
Sep 22 '06 #1
5 1357
sb,

If you are a newbie to Regex, than you have in my idea to avoid it or try to
learn it.

These newsgroups are meant to help you learn to fish, not to give you fish,
for that you have to buy them in a regular shop.

RegexLib
http://www.regexlib.com/Default.aspx

Expresso
http://www.ultrapico.com/Expresso.htm

I hope this helps a little bit?

Cor

"sb" <sb@yahoo.comschreef in bericht
news:%2****************@TK2MSFTNGP02.phx.gbl...
Hello,
I have a text file which contains plain text with the normal
carriage-return/linefeed line terminators. With that file I want to find
any occurence of "%R" (case-sensitive) on any line that does _not_ start
with "#"....so that I can replace it with something else later using
Regex.Replace().

An example file would look something like this (the real file is
~100kb..several hundred lines):

--- start of file ---

# description line...if there are any %R's on this line, ignore them\r\n
hey %Rthere\r\n
how's it %Rgoin?\r\n
%Rfine, you?\r\n

--- end of file ---

In the above, the regular expression should match all occurences of %R
except the one on the line that starts with "#". Can someone tell me what
the right regex search string would be? I'm sure this is very easy to do
but I'm still a newbie to regex.

Thanks in advance!
sb

Sep 22 '06 #2
sb <sb@yahoo.comwrote:
I have a text file which contains plain text with the normal
carriage-return/linefeed line terminators. With that file I want to find
any occurence of "%R" (case-sensitive) on any line that does _not_ start
with "#"....so that I can replace it with something else later using
Regex.Replace().

An example file would look something like this (the real file is
~100kb..several hundred lines):

--- start of file ---

# description line...if there are any %R's on this line, ignore them\r\n
hey %Rthere\r\n
how's it %Rgoin?\r\n
%Rfine, you?\r\n

--- end of file ---

In the above, the regular expression should match all occurences of %R
except the one on the line that starts with "#". Can someone tell me what
the right regex search string would be? I'm sure this is very easy to do
but I'm still a newbie to regex.
I wouldn't use regex at all. The regular expression is going to be
harder to understand than straight calls to String methods. Assuming
you've already read a line (with StreamReader.ReadLine) you can just
do:

if (!line.StartsWith ("#") && line.Contains ("%R"))

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 22 '06 #3
sb
Thanks for the response Jon. I coded it your way before I posted...which
works fine of course. However, I think I over-simplified my original post
:)

I'm essentially building an rtf file from a text file (generated by another
app...not mine) that contains a lot of proprietary tags like %R, %Y,
etc...which represent color tags. To build a proper rtf file color table, I
need to ensure that a color is actually used within the file. So in short,
I need to perform the replacements for each color tag and also know that at
least one replacement was actually made before I add that tag's color to the
color table. I realize I that I can accomplish this with simple string
functions...ie Replace(), IndexOf(), Contains(). However, I figured that
using a regex may be quicker (and more readable) in that I can do the
parsing once during the creation of a MatchCollection and then just perform
the replacements using the Groups within that Match Collection.

I admit that maybe I'm optimizing too early here :)

-sb
>
I wouldn't use regex at all. The regular expression is going to be
harder to understand than straight calls to String methods. Assuming
you've already read a line (with StreamReader.ReadLine) you can just
do:

if (!line.StartsWith ("#") && line.Contains ("%R"))

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Sep 23 '06 #4
sb <st********@yahoo.comwrote:
Thanks for the response Jon. I coded it your way before I posted...which
works fine of course. However, I think I over-simplified my original post
:)

I'm essentially building an rtf file from a text file (generated by another
app...not mine) that contains a lot of proprietary tags like %R, %Y,
etc...which represent color tags. To build a proper rtf file color table, I
need to ensure that a color is actually used within the file. So in short,
I need to perform the replacements for each color tag and also know that at
least one replacement was actually made before I add that tag's color to the
color table. I realize I that I can accomplish this with simple string
functions...ie Replace(), IndexOf(), Contains(). However, I figured that
using a regex may be quicker (and more readable) in that I can do the
parsing once during the creation of a MatchCollection and then just perform
the replacements using the Groups within that Match Collection.

I admit that maybe I'm optimizing too early here :)
It *may* be faster to use a regular expression - but I wouldn't worry
about that until you've got a working implementation to start with. It
would possibly be more readable to use a regular expression if the
reader is very familiar with regular expressions - but *much* harder
for people who don't use regular expressions very often.

I suggest you write the code in the most obvious way, using Replace,
IndexOf etc, get all your unit tests in place (preferrably doing that
before implementing the code, in fact) and then you can safely change
to using regular expressions if you feel there'll be a benefit.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 23 '06 #5
sb
Ok, thanks for the advice Jon. I'll leave it as-is for now :)

-sb

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP***********************@msnews.microsoft.co m...
sb <st********@yahoo.comwrote:
>Thanks for the response Jon. I coded it your way before I posted...which
works fine of course. However, I think I over-simplified my original
post
:)

I'm essentially building an rtf file from a text file (generated by
another
app...not mine) that contains a lot of proprietary tags like %R, %Y,
etc...which represent color tags. To build a proper rtf file color
table, I
need to ensure that a color is actually used within the file. So in
short,
I need to perform the replacements for each color tag and also know that
at
least one replacement was actually made before I add that tag's color to
the
color table. I realize I that I can accomplish this with simple string
functions...ie Replace(), IndexOf(), Contains(). However, I figured that
using a regex may be quicker (and more readable) in that I can do the
parsing once during the creation of a MatchCollection and then just
perform
the replacements using the Groups within that Match Collection.

I admit that maybe I'm optimizing too early here :)

It *may* be faster to use a regular expression - but I wouldn't worry
about that until you've got a working implementation to start with. It
would possibly be more readable to use a regular expression if the
reader is very familiar with regular expressions - but *much* harder
for people who don't use regular expressions very often.

I suggest you write the code in the most obvious way, using Replace,
IndexOf etc, get all your unit tests in place (preferrably doing that
before implementing the code, in fact) and then you can safely change
to using regular expressions if you feel there'll be a benefit.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Sep 23 '06 #6

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

Similar topics

8
by: Johnny | last post by:
I need to determine whether a text box contains a value that does not convert to a decimal. If the value does not convert to a decimal, I want to throw a MessageBox to have the user correct the...
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)...
2
by: John Grandy | last post by:
Is it advisable to compile a Regex for a massively scalable ASP.NET web-application ? How exactly does this work ? Do you create a separate class library and expose the Regex.Replace() as a...
0
by: delta7 | last post by:
Hi, I'm now nearing the completion of my first c# development. I have two applications. The first is a simple database app that uses an Access database to store Regex strings along with a...
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 ...
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
2
by: Good Man | last post by:
Hi there I have a series of HTML tables (well-formed, with elements ID'd quite nicely) and I need to extract the contents from certain TDs. For example, I'd like to get "Hi Mom!" from the...
4
by: seberino | last post by:
I'm looking over the docs for the re module and can't find how to "NOT" an entire regex. For example..... How make regex that means "contains regex#1 but NOT regex#2" ? Chris
3
by: =?Utf-8?B?TWFya19C?= | last post by:
The following is working for me but I want to include numbers in scientific notation. public double Evaluate( string expr ) { const string Num = @"(\-?\d+\.?\d*|\-?\.\d+)" Regex reMulDiv = new...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...
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.