非字母和非数字的正则表达式 [英] Regex for non-alphabets and non-numerals

查看:261
本文介绍了非字母和非数字的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请提供一种在C#.NET中编写正则表达式的解决方案:我需要针对非字母(a到z; A到Z)和非数字(0到9)的RegEx.表示要获取除字母和数字(0到9)以外的正则表达式的相反方法.

Please provide a solution to write a regular expression as following in C#.NET: I would require a RegEx for Non-Alphabets(a to z;A to Z) and Non-Numerals(0 to 9). Mean to say as reverse way for getting regular expression other than alphabets and otherthan numerals(0 to 9).

请提出相同的解决方案.

Kindly suggest the solution for the same.

推荐答案

您可以使用 否定字符类 此处:

You can use a negated character class here:

[^a-zA-Z0-9]

以上正则表达式将匹配单个字符,该字符不能为拉丁文的小写或大写字母或数字.

Above regex will match a single character which can't be a latin lowercase or uppercase letter or a number.

字符类开始处的 ^ ( [] 之间的部分)取反完整的类,以便它与类中的所有 not 匹配,而不是正常的字符类行为.

The ^ at the start of the character class (the part between [ and ]) negates the complete class so that it matches anything not in the class, instead of normal character class behavior.

要使其有用,您可能需要其中之一:

To make it useful, you probably want one of those:

  1. 零个或更多此类字符

  1. Zero or more such characters

[^a-zA-Z0-9]*

星号( * )表示前面的部分可以重复零次或多次.

The asterisk (*) here signifies that the preceding part can be repeated zero or more times.

一个或多个这样的字符

[^a-zA-Z0-9]+

此处的加号( + )表示前一部分可以重复一次或多次.

The plus (+) here signifies that the preceding part can be repeated one or more times.

一个完整的(可能为空)字符串,仅包含此类字符

A complete (possibly empty) string, consisting only of such characters

^[^a-zA-Z0-9]*$

此处的字符 ^ $ 的含义为 ,分别匹配字符串的开头和结尾.这样可以确保整个字符串由不在该字符类中的字符组成,并且在它们之前或之后没有其他字符.

Here the characters ^ and $ have a meaning as anchors, matching the start and end of the string, respectively. This ensures that the entire string consists of characters not in that character class and no other characters come before or after them.

一个完整的(非空)字符串,仅包含此类字符

A complete (non-empty) string, consisting only of such characters

^[^a-zA-Z0-9]+$

详细说明一下,这不会(也无法)确保您不会使用任何其他字符,可能来自其他脚本.字符串аеΒ在上面的正则表达式中将完全有效,因为它使用了希腊语和西里尔字母.此外,还有其他陷阱.字符串á将传递到正则表达式之上,而字符串 ́a 则不会(因为它是根据字母a和一个变音符号来构造字母á).

Elaborating a bit, this won't (and can't) make sure that you won't use any other characters, possibly from other scripts. The string аеΒ would be completely valid with the above regular expression, because it uses letters from Greek and Cyrillic. Furthermore there are other pitfalls. The string á will pass above regular expression, while the string ́a will not (because it constructs the letter á from the letter a and a combining diacritical mark).

因此有时必须小心处理否定的字符类.

So negated character classes have to be taken with care at times.

如果我想: ١٢٣ :-)

您可以使用字符类

[^\p{L&}\p{Nd}]

如果您需要注意上述事项.

if you need to take care of the above things.

这篇关于非字母和非数字的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆