正则表达式模式 - 允许字母数字、一堆特殊字符,但不允许特定的字符序列
我有以下正则表达式:
(?!^[&#]*$)^([A-Za-z0-9-'.,&@:?!()$#/\\]*)$
所以允许 AZ、aZ、0-9 和这些特殊字符 '.,&@:?!()$#/\
如果以下内容我想不匹配在字符串中的任何位置都会按以下顺序遇到字符集:
&#
当我仅使用“&#”作为输入运行此正则表达式时,它与我的模式不匹配,我收到一个错误,很好。 当我使用 '.,&@:?!()$#/\ABC123
运行正则表达式时,它确实符合我的模式,没有错误。
但是,当我运行它时:
'.,&#@:?!()$#/\ABC123
它也不会出错。 我在检查 &# 序列时出错了。
谁能告诉我我做错了什么,我不擅长这些事情。
I have the following regex:
(?!^[]*$)^([A-Za-z0-9-'.,&@:?!()$#/\\]*)$
So allow A-Z, a-Z, 0-9, and these special chars '.,&@:?!()$#/\
I want to NOT match if the following set of chars is encountered anywhere in the string in this order:
When I run this regex with just "" as input, it does not match my pattern, I get an error, great. When I run the regex with '.,&@:?!()$#/\ABC123
It does match my pattern, no errors.
However when I run it with:
'.,@:?!()$#/\ABC123
It does not error either. I'm doing something wrong with the check for the sequence.
Can someone tell me what I've done wrong, I'm not great with these things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
借用匹配带引号的字符串的技术,从字符类中删除
&
,添加&
not 的替代方案,后跟#
,并允许字符串可选地以&
结尾:Borrowing a technique for matching quoted strings, remove
&
from your character class, add an alternative for&
not followed by#
, and allow the string to optionally end with&
:我实际上会分两部分进行:
[^A-Za-z0-9'\.&@:?!()$#^]
你没有提到你的语言,但如果是 C#:
I would actually do it in two parts:
[^A-Za-z0-9'\.&@:?!()$#^]
You didn't mention your language, but if in C#:
^((?!&#)[A-Za-z0-9-'.,&@:?!()$#/\\])*$
请注意最后一个 \被转义(加倍)
如果不在反引号中,SO 会自动将
\\
转换为 \^((?!&#)[A-Za-z0-9-'.,&@:?!()$#/\\])*$
note that the last \ is escaped (doubled)
SO automatically turns
\\
into \ if not in backticks假设 Perl 兼容的 RegExp
不匹配字符串 '&#':
尽管您不需要括号,因为您正在匹配整个字符串。
Assuming Perl compatible RegExp
To not match on the string '&#':
Although you don't need the parenthesis because you are matching the entire string.
仅供参考,虽然 Ben Blank 的正则表达式有效,但它比需要的更复杂。 我会这样做:
因为我使用了否定的前瞻而不是否定的字符类,所以正则表达式不需要任何额外的帮助来匹配字符串末尾的&符号。
Just FYI, although Ben Blank's regex works, it's more complicated than it needs to be. I would do it like this:
Because I used a negative lookahead instead of a negated character class, the regex doesn't need any extra help to match an ampersand at the end of the string.
我建议在条件中使用两个正则表达式:
我相信你的第二个“主”正则表达式
有几个错误:
\
字符是一个标记指示下一个字符是某种字符“类”的一部分(例如\n
= 是换行符)。 字符序列\]
实际上导致您的括号列表不被终止。您可能最好使用
注意斜杠字符由双斜杠表示。
+
字符表示至少一个被测试的字符必须与正则表达式匹配; 如果可以传递零长度字符串,请将+
替换为*
。I'd recommend using two regular expressions in a conditional:
I believe your second "main" regex of
has several errors:
\
character in regular expressions is a token indicating that the next character is part of some sort of "class" of characters (ex.\n
= is the line feed character). The character sequence\]
is actually causing your bracketed list not to be terminated.You may be better off using
Note that the slash character is represented by a double-slash.
The
+
character indicates that at least one character being tested has to match the regex; if it is fine to pass a zero-length string, replace the+
with a*
.