Replacing String Using Regular Expression

L

lucky

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
 
C

Cor Ligthert [MVP]

Lucky,

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

Cor
 
L

Lars Behrmann

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/
 
H

Herfried K. Wagner [MVP]

lucky said:
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.
 
K

Kevin Spencer

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.
 
L

Lucky

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
 
K

Kevin Spencer

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.
 
C

Cor Ligthert [MVP]

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
 
G

Greg Bacon

: 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
 
L

Lucky

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).
 
J

Jim Underwood

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.
 
L

Lucky

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top