您可以使用兩個正則表達式。首先使用"\t|<bullet>"
(其中<bullet>
代表子彈的表示),首先使用空格替換TAB和子彈(" "
)。第二種模式是包含有效字符列表的否定字符集,用於替換無效字符的空字符串(""
),即刪除它們。因爲你需要保持CR和LF字符(空格),必須將它們添加到該組的有效字符:
using System;
using System.Text.RegularExpressions;
static class Program
{
public static void Main()
{
string pattern1 = @"\t";
Regex regex1 = new Regex(pattern1, RegexOptions.Compiled);
string pattern2 = @"[^A-Za-z0-9~!#$^&*()_+|`\-=\\{}:"">?<\[\];',./ \r\n]";
Regex regex2 = new Regex(pattern2, RegexOptions.Compiled);
string input = "ABZabz09~!#$^&*()_+|`-=\\{}:\">?<[];',./ \r\nárvíztűrő\ttükörfúrógép";
string temp = regex1.Replace(input, " ");
string output = regex2.Replace(temp, "");
Console.WriteLine(input);
Console.WriteLine(output);
Console.ReadKey(true);
}
}
輸出:
ABZabz09~!#$^&*()_+|`-=\{}:">?<[];',./
árvíztűrő tükörfúrógép
ABZabz09~!#$^&*()_+|`-=\{}:">?<[];',./
rvztr tkrfrgp
注意árvíztűrő
後的TAB被替換一個空間。
關於子彈:
我在Word中製作項目符號列表,並將它複製到網頁中的文本區域。然後我保存了HTML並發現子彈保存爲UTF-8編碼字符E280A2
。這就是我在「子彈的代表」之上所稱的。你應該找出可能的符號字符的二進制表示,並將其添加到第一個模式:一方或他們的TAB字符,或者把所有的人都爲一個字符集:
using System;
using System.Text;
using System.Text.RegularExpressions;
static class Program
{
public static void Main()
{
byte[] bulletBytes = new byte[] { 0xE2, 0x80, 0xA2 };
string bullet= Encoding.UTF8.GetString(bulletBytes);
string pattern1 = @"[\t" + bullet + "]";
Regex regex1 = new Regex(pattern1, RegexOptions.Compiled);
string pattern2 = @"[^A-Za-z0-9~!#$^&*()_+|`\-=\\{}:"">?<\[\];',./ \r\n]";
Regex regex2 = new Regex(pattern2, RegexOptions.Compiled);
string input =
bullet + "ABZabz09~!#$^&*()_+|`-=\\{}:\">?<[];',./ \r\n" +
bullet + "árvíztűrő\ttükörfúrógép";
string temp = regex1.Replace(input, " ");
string output = regex2.Replace(temp, "");
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine(input);
Console.WriteLine(output);
Console.ReadKey(true);
}
}
輸出(你應該改變控制檯字體爲Lucida Console看到子彈):
•ABZabz09~!#$^&*()_+|`-=\{}:">?<[];',./
•árvíztűrő tükörfúrógép
ABZabz09~!#$^&*()_+|`-=\{}:">?<[];',./
rvztr tkrfrgp
現在除了TAB,在每行開頭的子彈也被替換爲空。
來源
2013-02-13 13:11:02
kol
我知道這並不完全是你想要的,但是如何在'textarea'上啓用所見即所得的編輯器,以便從Word中粘貼選項。 [TinyMCE](http://www.tinymce.com/wiki.php/Plugin:paste)只有這個任務的插件? – Tr1stan 2013-02-13 12:54:31
截至目前,這不是我的選擇。 – NoobDeveloper 2013-02-13 12:55:39
使用'ASCII' SQL函數來找出它到底是什麼字符,我的猜測是你正在尋找錯誤的:http://msdn.microsoft.com/en-us/library/ms177545.aspx – Arran 2013-02-13 13:01:17