java 正则表达式阻止某些特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12493011/
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
Regex to block certain special characters
提问by ukanth
I'm currently stuck at completing the following regex.
我目前坚持完成以下正则表达式。
My Regex
我的正则表达式
^[a-zA-Z0-9.][a-zA-Z0-9.+:_-]+[a-zA-Z0-9.]$
and the matching structure is Sample:Te.st4:Test.Sample
each name is separated with :
But I want to allow the each name to have any special characters other than the following ones.
并且匹配的结构是 Sample:Te.st4:Test.Sample
每个名称都用 分隔:
但是我希望每个名称都可以包含除以下字符以外的任何特殊字符。
> # *
I don't know how to write that regex. Please help me to resolve this.
我不知道如何编写该正则表达式。请帮我解决这个问题。
回答by JoeG
The regex "^[^>#*]+$"
will match any input apart from something containing >
, #
or *
.
"^[^>#*]+$"
除了包含>
,#
或 的内容外,正则表达式将匹配任何输入*
。
From your existing regex it looks like you don't want to allow :
to be the first or last character, in which case the regex you want is this:
从您现有的正则表达式看来,您不希望:
成为第一个或最后一个字符,在这种情况下,您想要的正则表达式是:
"^[^:>#*]+|([^:>#*][^>#*]+[^:>#*])$"
"^[^:>#*]+|([^:>#*][^>#*]+[^:>#*])$"