替换为正则表达式

Noo*_*per 8 c# regex

在我们的应用程序中,用户从MS word输入数据到asp.net textarea控件,最后数据保存在SQL Server中.出于某种原因,从SQL Server Management Studio查看时,几乎没有垃圾字符看起来像小方块.

这会在生成Crystal Reports时导致错误.

我需要一个正则表达式,它将删除所有这些字符和子弹.唯一有效的输入是

A-Z, a-z , 0-9, ~ ! @ # % $ ^ & *  ( ) _ + | ` - = \ {}:">? < [ ] ; ' , . /
Run Code Online (Sandbox Code Playgroud)

此外,标签空间应替换为单个空格.允许输入键或新行.

目前我正在使用

Regex.Replace(data, @"[^\u0000-\u007F]", " ");
Run Code Online (Sandbox Code Playgroud)

但它不能删除项目符号或制表符空格.

任何正则表形的忍者可以帮助我解决这个问题吗?提前致谢.

kol*_*kol 3

您可以使用两个正则表达式。第一种,首先使用模式"\\t|<bullet>"(其中<bullet>代表项目符号的表示),用空格 ( " ") 替换制表符和项目符号。第二个,其模式是包含有效字符列表的否定字符集,第二个用于用空字符串("")替换无效字符,即删除它们。由于您需要保留 CR 和 LF 字符(和空格),因此必须将它们添加到有效字符集中:

\n\n
using System;\nusing System.Text.RegularExpressions;\n\nstatic class Program\n{\n  public static void Main()\n  {\n    string pattern1 = @"\\t";\n    Regex regex1 = new Regex(pattern1, RegexOptions.Compiled);\n    string pattern2 = @"[^A-Za-z0-9~!#$^&*()_+|`\\-=\\\\{}:"">?<\\[\\];\',./ \\r\\n]";\n    Regex regex2 = new Regex(pattern2, RegexOptions.Compiled);\n\n    string input = "ABZabz09~!#$^&*()_+|`-=\\\\{}:\\">?<[];\',./ \\r\\n\xc3\xa1rv\xc3\xadzt\xc5\xb1r\xc5\x91\\tt\xc3\xbck\xc3\xb6rf\xc3\xbar\xc3\xb3g\xc3\xa9p";\n    string temp = regex1.Replace(input, " ");\n    string output = regex2.Replace(temp, "");\n    Console.WriteLine(input);\n    Console.WriteLine(output);\n    Console.ReadKey(true);\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
ABZabz09~!#$^&*()_+|`-=\\{}:">?<[];\',./\n\xc3\xa1rv\xc3\xadzt\xc5\xb1r\xc5\x91       t\xc3\xbck\xc3\xb6rf\xc3\xbar\xc3\xb3g\xc3\xa9p\nABZabz09~!#$^&*()_+|`-=\\{}:">?<[];\',./\nrvztr tkrfrgp\n
Run Code Online (Sandbox Code Playgroud)\n\n

请注意,后面的 TAB\xc3\xa1rv\xc3\xadzt\xc5\xb1r\xc5\x91被替换为单个空格。

\n\n

关于子弹:

\n\n

我在 Word 中制作了项目符号列表并将其复制到网页的文本区域中。然后我保存了 HTML,发现项目符号被保存为 UTF-8 编码的字符E280A2。这就是我上面所说的“子弹的表示”。您应该找出可能的项目符号字符的二进制表示形式,并将它们添加到第一个模式:或者将它们与 TAB 字符进行“或”操作,或者将它们全部放入字符集中:

\n\n
using System;\nusing System.Text;\nusing System.Text.RegularExpressions;\n\nstatic class Program\n{\n  public static void Main()\n  {\n    byte[] bulletBytes = new byte[] { 0xE2, 0x80, 0xA2 };\n    string bullet= Encoding.UTF8.GetString(bulletBytes);\n\n    string pattern1 = @"[\\t" + bullet + "]";\n    Regex regex1 = new Regex(pattern1, RegexOptions.Compiled);\n    string pattern2 = @"[^A-Za-z0-9~!#$^&*()_+|`\\-=\\\\{}:"">?<\\[\\];\',./ \\r\\n]";\n    Regex regex2 = new Regex(pattern2, RegexOptions.Compiled);\n\n    string input = \n      bullet + "ABZabz09~!#$^&*()_+|`-=\\\\{}:\\">?<[];\',./ \\r\\n" + \n      bullet + "\xc3\xa1rv\xc3\xadzt\xc5\xb1r\xc5\x91\\tt\xc3\xbck\xc3\xb6rf\xc3\xbar\xc3\xb3g\xc3\xa9p";\n    string temp = regex1.Replace(input, " ");\n    string output = regex2.Replace(temp, "");\n    Console.OutputEncoding = Encoding.UTF8;\n    Console.WriteLine(input);\n    Console.WriteLine(output);\n    Console.ReadKey(true);\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出(您应该将控制台字体更改为 Lucida Console 才能看到项目符号):

\n\n
\xe2\x80\xa2ABZabz09~!#$^&*()_+|`-=\\{}:">?<[];\',./\n\xe2\x80\xa2\xc3\xa1rv\xc3\xadzt\xc5\xb1r\xc5\x91      t\xc3\xbck\xc3\xb6rf\xc3\xbar\xc3\xb3g\xc3\xa9p\n ABZabz09~!#$^&*()_+|`-=\\{}:">?<[];\',./\n rvztr tkrfrgp\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在除了 TAB 之外,每行开头的项目符号也被替换为空格。

\n