0

我需要一个不允许字符的正则表达式。

^[A-Z0-9 _]*[A-Z0-9][A-Z0-9 _]*$

这些似乎不起作用,因为我需要转义任何类型的字符(英语、中文、希腊语等)

4

3 回答 3

1

If you only want to accept digits and the . character using POSIX (PHP):

^[.1-9]*$

If you want to explicit exclude word characters

^[^\w]*$

This will match anything but word characters.

Note (that as has been replied in the comments):

  • this will work with JGsoft, .NET, Perl, Python.
  • It will not work if using Java, JavaScript, PCRE, Ruby, POSIX.

You edited your post and stated that you use PHP. As far as I know it uses POSIX ERE. In that case this solution will not work. However I keep my answer for the search functionality in case another user (that does use the supported engines) finds this answer.

于 2013-01-14T15:38:49.170 回答
1

使用\p{L}\p{Letter}匹配任何语言的任何 (Unicode) 字母。要匹配任何非字母,您应该使用:\p{^L}\P{L}(取决于您使用的正则表达式引擎)来否定。

于 2013-01-14T15:40:30.797 回答
0

任何字符 \w
不是字符 \W

于 2013-01-14T15:45:03.040 回答