vb.net 限制扩展 ASCII 字符集的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13970327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:37:02  来源:igfitidea点击:

Regular expression to restrict Extended ASCII character set

regexvb.netvalidation

提问by Munawar

I have multi lingual application which creates xml files but Extended ASCII characters from 168 to 254 (?????????▓│┤╡╢╖╕╣║╗╜╛┐└┴┬├) are not supposed in XML tags so, I would like to restrict user from entering.

我有创建 xml 文件的多语言应用程序,但从 168 到 254 的扩展 ASCII 字符(?????????▓│┤╡╢╖╕╣║╗╜╛┐└┴┬├)不应该在 XML 中标签,我想限制用户进入。

I tried restricting everything besides alphanumeric, underscore and dash but it would not allow accented characters ó ? ? which are part of extended ASCII. Here is regx "^[a-zA-Z0-9\s.\-_]+$"

我尝试限制除字母数字、下划线和破折号之外的所有内容,但它不允许重音字符 ó ?? 它们是扩展 ASCII 的一部分。这是正则"^[a-zA-Z0-9\s.\-_]+$"

Second option was to create a string of all symbols from 168 to 254 and check if string contains any of them but not sure if it is reliable and accurate solution.

第二种选择是创建一个包含从 168 到 254 的所有符号的字符串,并检查字符串是否包含其中任何一个,但不确定它是否是可靠和准确的解决方案。

What is best way to filter input for Extended ASCII character set ?

过滤扩展 ASCII 字符集输入的最佳方法是什么?

Linkto Extended ASCII character set table

链接到扩展 ASCII 字符集表

回答by Rohit Jain

Rather you can make use of rangein character class, to exclude specific range of characters using their Hex Codes: -

相反,您可以使用range字符类,使用它们来排除特定范围的字符Hex Codes:-

[^\xA8-\xFE]

The above regex will match any character except those in the given range. Those are the hex codes for the range you posted - [168, 254]

上述正则表达式将匹配除给定范围内的字符之外的任何字符。这些是您发布的范围的十六进制代码 -[168, 254]

回答by Munawar

Although @Oded suggest was applicable but I used following solution:

虽然@Oded 建议适用,但我使用了以下解决方案:

Dim filteredInput as string

Private const XML_RESTRICTED_CHARACTERS as string ="[????????○?♂♀????????§??↑↓→←∟?▲▼#$%&()*+,-./:;<=>?@[\]^_`¢£¥??ao??????????▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌??α?ΓπΣσμτΦΩδ∞φε∩≡±≥≤??÷≈°?·√?2■""}{]"

filteredInput =Regex.Replace(strInput.ToLower(), XML_RESTRICTED_CHARACTERS, "")

回答by Oded

Second option was to create a string of all symbols from 168 to 254 and check if string contains any of them but not sure if it is reliable and accurate solution.

第二种选择是创建一个包含从 168 到 254 的所有符号的字符串,并检查字符串是否包含其中任何一个,但不确定它是否是可靠和准确的解决方案。

Yes, this is a reliable and accurate solution. It is also more lightweight than regular expressions.

是的,这是一个可靠且准确的解决方案。它也比正则表达式更轻量级。