470,624 Members | 2,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,624 developers. It's quick & easy.

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 1988
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Dimitris Georgakopuolos | last post: by
32 posts views Thread by tshad | last post: by
3 posts views Thread by James D. Marshall | last post: by
20 posts views Thread by Opettaja | last post: by
reply views Thread by Lakhi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.