简体   繁体   English

C#特殊字符

[英]C# special characters

I need to verify that a string doesn't contain any special characters like #,%...™ etc. Basically it's a Name/surname (and some similar) strings, however, sticking to [a-zA-Z] wouldn't do as symbols like ščřž... are allowed. 我需要验证一个字符串是否不包含任何特殊字符,例如#,%...™等。基本上,它是一个名称/姓氏(和一些类似的)字符串,但是,坚持使用[a-zA-Z]不会请勿使用ščřž...之类的符号。

At the moment I'd go with somewhat like 此刻我会有点喜欢

bool NonSpecial(string text){
    return !Regex.Match(Regex.Escape("!#@$%^&......")).Success;
}

but that just seems to be too complicated and clumsy. 但这似乎太复杂又笨拙。

Is there any simpler and/or more elegant way? 有没有更简单和/或更优雅的方式?

Update: So after reading all the replies I decided to go with 更新:所以在阅读了所有回复之后我决定一起去

private bool IsName( string text ) {
    return Regex.Match( text, @"^[\p{L}\p{Nd}'\.\- ]+$" ).Success && !Regex.Match( text, @"['\-\.]{2}" ).Success && !Regex.Match( text, "  " ).Success;
}

Basically the name can contain Letters, numbers, ', ., -, and spaces, any of the ",.-" must be separeted by at least 1 other allowed characters and there cannot be 2 spaces in a row. 基本上,名称可以包含字母,数字,',。,-和空格,任何“,.-”都必须由至少1个其他允许的字符分隔,并且行中不能有2个空格。

Hope that's correct. 希望这是对的。

You can use the Unicode category for letters: 您可以将Unicode类别用于字母:

Regex.Match(text, @"\p{L}+");

See Supported Unicode Categories . 请参阅支持的Unicode类别

This problem is worse than you imagine. 这个问题比你想象的还要糟糕。

There are literally thousands of allowable characters that can legitimately be part of a name, spread over hundreds of ranges in the various unicode alphabets. 实际上有数千个允许的字符可以合法地成为名称的一部分,分布在各种unicode字母表中的数百个范围内。

There are also literally tens of thousands of characters that will never be part of a name. 还有成千上万的字符永远不会成为名称的一部分。 Think of all the emoji and ascii art characters. 想想所有的表情符号和ascii艺术角色。 These are also spread over hundreds of separate ranges of unicode characters. 这些也分布在数百个不同范围的unicode字符中。

Sifting the wheat from the chaff via manual code, even regular expressions, just isn't going to work well. 通过手动代码,甚至是正则表达式来筛选谷壳中的小麦,效果不佳。

Thankfully, this work has been done for you. 幸运的是,这项工作已经为您完成。 Look at the char.IsLetter() method. 查看char.IsLetter()方法。

You may also want to have an exception for the various allowed separator characters and accents that are not letters, but can be part of a name: hyphens, apostrophe's, and periods are legitimate, and all have more than one allowed unicode encoding. 您可能还希望对各种允许的分隔符和重音(不是字母,但是可以是名称)进行例外处理:连字符,撇号和句点是合法的,并且都具有多个允许的unicode编码。 Unfortunately, I don't have a quick solution for you here. 不幸的是,我这里没有快速解决方案。 This may have to a best-effort approach, looking at just some of the more common. 这可能需要尽力而为,只考虑一些更常见的方法。

try using Linq/Lambda as well pretty straight forward 尝试使用Linq / Lambda以及非常简单的方法

will return true if it doesn't contain letters 如果它不包含字母,则返回true

bool result = text.Any(x => !char.IsLetter(x));

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

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