472,958 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Replacing String Using Regular Expression

hi,
i got file which contains "----------------" in a line. the line only
contains this data as a saperation. using regular expression i want to
i detify the line contains that data and replace with spaces.
if anyone has any idea,solution or link plz do share with me.

thans in advt.

Lucky

Nov 17 '05 #1
11 2101
Lucky,

Is there any reason that you want to do this with a Regular Expression and
not with a normal replace command?

Cor
Nov 17 '05 #2
Hi Lucky,

if your separtor length is always the same this
regex will work for you.

string content = null;
using (StreamReader sr = new StreamReader("Test.txt"))
{
String line = null;
while ((line = sr.ReadLine()) != null)
{
//if seperator line length 10
Regex rgx = new Regex("^-(-){8}-$");
if(rgx.IsMatch(line.Replace(@"\n","")))
continue;
content += line;
}
}

Cheers
Lars Behrmann

_________________
Nothing is impossible. UML is the key for all your problems.
AODL - Make your .net apps OpenOffice ready
http://aodl.sourceforge.net/
lucky schrieb:
hi,
i got file which contains "----------------" in a line. the line only
contains this data as a saperation. using regular expression i want to
i detify the line contains that data and replace with spaces.
if anyone has any idea,solution or link plz do share with me.

thans in advt.

Lucky


Nov 17 '05 #3
"lucky" <tu************@gmail.com> schrieb:
i got file which contains "----------------" in a line. the line only
contains this data as a saperation. using regular expression i want to
i detify the line contains that data and replace with spaces.


I think that regular expressions are far oversized for this problem
(interesting article on this topic:
<URL:http://msmvps.com/jon.skeet/archive/2005/09/21/67247.aspx>). Check out
both the methods of the 'String' class and the members of the 'Strings'
module, more specific the 'Replace' methods.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 17 '05 #4
If the string is always the same, the same characters, and length, there is
no need for a Regular Expression. Regular Expressions look for patterns. You
are looking for a specific substring. Use String.Replace.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"lucky" <tu************@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
hi,
i got file which contains "----------------" in a line. the line only
contains this data as a saperation. using regular expression i want to
i detify the line contains that data and replace with spaces.
if anyone has any idea,solution or link plz do share with me.

thans in advt.

Lucky

Nov 17 '05 #5
well my problem is that, i dont know the size of the string. it could
be 80 or 800 or 8000. main purpose of the line is to saperate data
lines. but i'm not getting how can i replace such a big line in one
shot though i've done work around but if still some one can help me to
find a short way of replacing a whole line would be appriciated.

Lucky

Nov 17 '05 #6
My problem is that I can't tell from your message what the context of it is.
My newsreader hides read messages by default, and I really don't like having
to switch back and forth from hding to viewing to hiding again. It requires
a lot of trouble and time to do so. Not so much for a single thread, but
multiply that by the number of messages and threads I read every day, and
you have a significant waste of time, which is unfortunately, quite limited
to me, and others who are working at successful jobs.

Now, this is fairly common among people who regularly use newsgroups. And if
you make it more difficult for someone to figure out what you're asking, as
time is limited, many people will simply go on to another question that
requires less time and trouble to answer. So, if I have a question, I want
to make sure that as many people as possible, who may be able to answer it,
will answer it. This means that I do everything I can to make them work
less. I include as many facts and data about a problem as I can. If I am
continuing a thread, I will quote the necessary previous messages in the
thread as contribute to the facts needed to solve the problem.

Think of it like fishing. There are only so many fish in the pond. You want
to use bait that will attract as many as possible. Hopefully, one of them
will be the big fish you're looking to catch.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Lucky" <tu************@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
well my problem is that, i dont know the size of the string. it could
be 80 or 800 or 8000. main purpose of the line is to saperate data
lines. but i'm not getting how can i replace such a big line in one
shot though i've done work around but if still some one can help me to
find a short way of replacing a whole line would be appriciated.

Lucky

Nov 17 '05 #7
Lucky,

You can try this, however, check that this is possible in your situation.

\\\
Dim str As String = "-----------------------"
str = str.Replace("--", " ")
str = str.Replace(" -", " ")
///
I hope this helps,

Cor
Nov 17 '05 #8
In article <11**********************@g47g2000cwa.googlegroups .com>,
lucky <tu************@gmail.com> wrote:

: i got file which contains "----------------" in a line. the line only
: contains this data as a saperation. using regular expression i want to
: i detify the line contains that data and replace with spaces.
: if anyone has any idea,solution or link plz do share with me.

Does the code below help?

static void Main(string[] args)
{
string[] inputs = new string[]
{
"Every", "-", "Good", "--", "Boy", "---",
"Does", "----", "Fine", "--------------",
};

Regex alldashes = new Regex(@"^(-+)$");
MatchEvaluator zap = new MatchEvaluator(BlankDashes);
foreach (string line in inputs)
Console.WriteLine(
"line = [" + alldashes.Replace(line, zap) + "]");
}

private static string BlankDashes(Match m)
{
return m.Groups[1].Value.Replace("-", " ");
}

Greg
--
Comfort rides shotgun with the temporary.
-- Daniel Weinshenker
Nov 17 '05 #9
thanks to all you guys. b'coz of your help i've solved the problem in
both ways. by regular expression(i learnt a lot) and with string member
functions(now i got some more mastery on those functions).

Nov 17 '05 #10
Can you please post the code you ended up using for both approaches? For
others with the same problem it will be helpful to see both solutions
together in this post.

Just in general it is nice to be able to see how problems were solved.
"Lucky" <tu************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
thanks to all you guys. b'coz of your help i've solved the problem in
both ways. by regular expression(i learnt a lot) and with string member
functions(now i got some more mastery on those functions).

Nov 17 '05 #11
sorry dude but after i solved the problem my project manager had
scraped the solution and i dont have any backup of solution but go
through the thread discussion coz i had developed the solution on the
basis of the discussion. pleople had helped me a lot so i think same
thing will work for you. you have to taken pain to read all postings

Nov 17 '05 #12

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

Similar topics

11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
0
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
2
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
11
by: lucky | last post by:
hi, i got file which contains "----------------" in a line. the line only contains this data as a saperation. using regular expression i want to i detify the line contains that data and replace...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
0
by: Lakhi | last post by:
hi frnds, I need small help in String replacement I need to replace the text using replceAll() with Case-Insensitve . Is there any regular expression for this? i have this expression ...
0
by: jambalapamba | last post by:
hi I am using regular expression to replace all the hardcoded strings in html page e.g.eg: http://google.com/aref/film.html should be replace/film.html I am using reg expreesion...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.