使用正则表达式替换字符串 [英] Replacing String Using Regular Expression

查看:116
本文介绍了使用正则表达式替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我得到的文件包含----------------在一条线上。只有这一行

包含这个数据作为一个saperation。使用正则表达式我希望

i detify该行包含该数据并替换为空格。

如果有人有任何想法,解决方案或链接请与我分享。


提示。


幸运

解决方案

< blockquote>幸运,


你是否有任何理由要使用正则表达式和

而不是正常的替换命令?


Cor


嗨幸运,


如果你的separtor长度总是相同的这个

正则表达式适合你。


string content = null;

using(StreamReader sr = new StreamReader(" Test.txt") ;))

{

String line = null;

while((line = sr.ReadLine())!= null)

{

//如果分隔线长度10

Regex rgx =新正则表达式(" ^ - ( - ){8} -

");

if(rgx.IsMatch(line。替换(@\ n,")))

继续;

内容+ =行;

} < br $>
}


干杯

Lars Behrmann


_________________

没有什么是不可能的。 UML是解决所有问题的关键。

AODL - 让您的.net应用程序OpenOffice准备就绪
http://aodl.sourceforge.net/

lucky schrieb:

hi,
我得到的文件包含----------------在一条线上。该行仅包含此数据作为一个分类。使用正则表达式我希望
我解除包含该数据的行并用空格替换。
如果有任何想法,解决方案或链接请与我分享。

在建议。

幸运




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

解决方案

Lucky,

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

Cor


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




这篇关于使用正则表达式替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆