Groups | Blog | Home
all groups > dotnet faqs > october 2005 >

dotnet faqs : Replacing String Using Regular Expression


Herfried K. Wagner [MVP]
10/22/2005 12:00:00 AM
"lucky" <tushar.n.patel@gmail.com> schrieb:
[quoted text, click to view]

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/>
lucky
10/22/2005 12:12:44 AM
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
Lars Behrmann
10/22/2005 1:16:32 AM
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:

[quoted text, click to view]
Kevin Spencer
10/22/2005 7:45:08 AM
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.

[quoted text, click to view]

Cor Ligthert [MVP]
10/22/2005 9:48:49 AM
Lucky,

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

Cor

AddThis Social Bookmark Button