简体   繁体   English

正则表达式允许带有特定特殊字符的字母数字点网

[英]Regex Allow alphanumeric with specific special characters dot net

I have big .txt file which I am reading using StreamReader (C# .NET). 我有一个很大的.txt文件,正在使用StreamReader (C#.NET)阅读。

My requirement is to replace any of the special characters not in the list to replace with space in entire string (multiple occurrences). 我的要求是替换列表中未包含的任何特殊字符,以整个字符串中的空格替换(多次出现)。

List of special characters allowed is & / - ' . ) ( 允许的特殊字符列表为& / - ' . ) ( & / - ' . ) ( . & / - ' . ) (

So far, I have tried this but it is not working the way I want it: 到目前为止,我已经尝试过了,但是它并没有按照我想要的方式工作:

aLine = Regex.Replace(aLine, "[^0-9A-Za-z().&'/-]+$", " ");

Your current regex is doing this: 您当前的正则表达式正在执行此操作:

String input = "123abc[]]]]]]456:$def";
String aLine = Regex.Replace(input, "[^0-9A-Za-z().&'/-]+$", " ");
//=> "123abc[]]]]]]456:$def" 
      ^^^^^^^^^^^^^^^^^^^^^ original string (did not replace)

Remove the end of string $ anchor: 删除字符串$ anchor的结尾:

String input = "123abc[]]]]]]456:$def";
String aLine = Regex.Replace(input, "[^0-9A-Za-z().&'/-]+", " ");
//=> "123abc 456 def"

If you want to leave a space for every instance, remove the quantifier: 如果要为每个实例保留一个空格,请删除量词:

String input = "123abc[]]]]]]456:$def";
String aLine = Regex.Replace(input, "[^0-9A-Za-z().&'/-]", " ");
//=> "123abc       456  def"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM