0

大家好,我需要一个只提取标点符号/字符的正则表达式。

到目前为止我有这个:

[._^%$#!~@,-]+

但如果至少有 1 个标点符号并且仍然允许任何其他字符(数字或字母),则此方法有效

我只需要允许标点符号/字符

4

4 回答 4

4

Try this:

^[\p{S}\p{P}]+$

\p{S} matches any symbol character, and \p{P} matches any punctuation.

Note that your pattern will not match all the symbols and punctuations not present in the list.

于 2012-11-21T12:19:25.010 回答
3

Try anchoring the regex to the start and the end of the string (unless you're using Multiline matching) - i.e. ^ at the beginning and $ at the end:

^[._^%$#!~@,-]+$

Note - this does not endorse your actual pattern (I can't say whether this is matching all the 'special characters' you're talking about, but it will make it so that the entire string must be all 'special'.

于 2012-11-21T12:19:48.230 回答
0

[^a-zA-Z0-9]* 你可以试试这样的。不应该接受那些字符,cba 在你输入的一个字符旁边写下所有字符。

于 2012-11-21T12:29:06.033 回答
0
\W

匹配任何不是单词字符的字符(字母数字和下划线)。

于 2012-11-21T14:20:53.160 回答