将正则表达式条件放在流写C#的位置 [英] Where to put regex conditions on streamwriting C#

查看:76
本文介绍了将正则表达式条件放在流写C#的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的代码中添加新的正则表达式,从列表中打印(使用streamreader)。

在哪里添加正则表达式,从文件和正则表达式中删除这些字符( - :_ |)以添加标题为文件。



正则表达式替换char:line.Replace(@ - :_ |,string.Empty);



正则表达式添加标题:



  using (StreamWriter file =  new  StreamWriter(fs))
{
file.WriteLine( 数字,用户,功能,消息);

for int index = 0 ; index < pr.Length; index ++)
{

file.WriteLine( {0},\{1} \,{2},{3}
pr [s] .Number,pr [s] .User,
pr [s] .Function,pr [s] .Message;
}
}





我尝试了什么:



 使用(StreamWriter writer =  new  StreamWriter(path2))
{
foreach string l in 不活动)
{

// line.Replace(@ - :_ | ,string.Empty);
writer.WriteLine(l);
}
}

解决方案

引用:

我想删除不替换那些字符



这就是用空字符串替换它们的方法 - 它会删除字符。

如果你想要删除所有这些字符,无论rgey出现在哪里,速度比单个字符串,你有错误的正则表达式。

你的正则表达式会改变这个

你好 - :_ |那里!

对此:

你好!

但是会保持不变:

你好 - :有!_ | 



如果要删除所有实例任何角色,你需要这个正则表达式:

 [ - :_ |] + 





获取 Expresso 的副本[ ^ ] - 它是免费的,它检查并生成正则表达式。



但该代码看起来非常像一个成员开始变成帮助吸血鬼/巨魔的代码,因为他拒绝学习任何事情并为自己思考......我在这里闻到了一个袜子傀儡......


你应该尝试替换

 @ - :_ |



with

 @[ - :_ |]





只是一些有趣的链接来帮助构建和调试RegEx。

以下是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

以下是帮助构建RegEx并调试它们的工具的链接:

.NET正则表达式测试程序 - 正则表达式风暴 [ ^ ]

Expresso正则表达式工具 [ ^ ]

RegExr:Learn,Build,&测试RegEx [ ^ ]

此节目RegEx是一个很好的图表,它非常有助于理解RegEx的作用:

Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]


你不能这样使用string.Replace(使用全局字符串);你必须使用string.Replace来替换每个字符。

示例:

 l = l.Replace(   -  )。替换(  )。替换(  _ )。替换(  | ); 



注意:

- string.Replace不会进行变量的就地编辑,你必须再次将方法的结果强制转换为变量( l = ... )。

- 您可以将调用链接到替换方法。

- 在代码块中你在评论行中表示,你似乎使用的 l 中定义的变量不匹配你的foreach循环。

还有其他选项可以做同样的事情,例如使用正则表达式,但我认为在其他一些帖子中已经有一些关于这个主题的指示。

希望这会有所帮助。麻烦。


I want to add new regex to my code, which prints from list (with streamreader).
Where to add regex which removes these characters( - : _ |) from file and regex to add header to the file.

regex of replacing char: line.Replace(@"- : _ |", string.Empty);

regex for adding header:

using (StreamWriter file = new StreamWriter(fs))                    
{
    file.WriteLine( "Number,User,Function,Message"); 

    for (int index = 0; index < pr.Length; index++)
    {
      
        file.WriteLine("{0},\"{1}\",{2},{3}",
           pr[s].Number, pr[s].User,
           pr[s].Function, pr[s].Message; 
    }
}



What I have tried:

using (StreamWriter writer = new StreamWriter(path2))
            {
                foreach (string l in inactive)
                {

           //line.Replace(@"- : _ |", string.Empty); 
                    writer.WriteLine(l);
}
}

解决方案

Quote:

i want to remove not replace those characters


That's what replacing them with an empty string does - it removes the characters.
If you want to remove all those characters regardless of where rgey appear, ratehr than as a single string, you have teh wrong regex.
Your regex will change this

Hello -:_|There!

To this:

Hello There!

But will leave this unchanged:

Hello -:There!_|


If you want to remove all instances of any of the characters, you need this regex:

[-:_|]+



Get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.

But that code is looking very much like the code used by a member who is starting to become rather a Help Vampire / Troll because he refuses to learn anything and think for himself ... I smell a sock puppet here...


You should try to replace

@"- : _ |"


with

@"[- : _ |]"



Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]


You cannot use string.Replace this way (using a global string); you have to use a string.Replace for each character to replace.
Example:

l = l.Replace("-", "").Replace(":", "").Replace("_", "").Replace("|", "");


Notes:
- string.Replace does not proceed to an in-place editing of the variable, you have to cast the result of the method to the variable again (l = ...).
- You can chain the calls to the Replace method.
- In the code block you showed, in the commented line, you seem to use a line which does not match the l variable defined in your foreach loop.
There are other options to do the same, using regular expressions for example, but I think some directions already have been given to you on this subject in some other posts.
Hope this helps. Kindly.


这篇关于将正则表达式条件放在流写C#的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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