【发布时间】:2015-02-05 19:51:16
【问题描述】:
我正在尝试使用正则表达式 (.Net) 来“清理”Unicode 输入字符串——要求是删除所有不可见字符/控制字符,除了 CR(回车)和 LF(换行符)。换句话说,保留所有有效的可打印字符(英语和法语),包括 CR 和 LF。
我已经尝试了以下(只是使用下划线来查看被替换的内容),但它也删除了 CR / LF ...
clean_str = Regex.Replace( in_str, "\p{C}+", "_" )
也试过了:
clean_str = Regex.Replace( in_str, "(\p{Cf}|\p{Co}|\p{Cs}|\p{Cn}|[\x00-\x09]|\x0b|\x0c|[\x0e-\x1f]|\x7f)+", "_" )
来自http://www.regular-expressions.info/unicode.html ...
p{C} or \p{Other}: invisible control characters and unused code points.
◦\p{Cc} or \p{Control}: an ASCII 0x00–0x1F or Latin-1 0x80–0x9F control character.
◦\p{Cf} or \p{Format}: invisible formatting indicator.
◦\p{Co} or \p{Private_Use}: any code point reserved for private use.
◦\p{Cs} or \p{Surrogate}: one half of a surrogate pair in UTF-16 encoding.
◦\p{Cn} or \p{Unassigned}: any code point to which no character has been assigned.
大师 - 如果您有更好/更有效的方法 - 请发布!
提前致谢!
【问题讨论】: