netbios 名称的正则表达式
我在弄清楚如何构建用于验证 netbios 名称的正则表达式时遇到了这个问题。根据ms标准这些字符是非法的 \/:*?"<>|
所以,这就是我想要检测的。我的正则表达式看起来像这样
^[\\\/:\*\?"\<\>\|]$
但是,那行不通。
任何人都可以指出我正确的方向吗?(请不是 regexlib.com。 ..) 如果重要的话,我将 php 与 preg_match 一起使用。
谢谢
I got this issue figuring out how to build a regexp for verifying a netbios name. According to the ms standard these characters are illegal
\/:*?"<>|
So, thats what I'm trying to detect. My regex is looking like this
^[\\\/:\*\?"\<\>\|]$
But, that wont work.
Can anyone point me in the right direction? (not regexlib.com please...)
And if it matters, I'm using php with preg_match.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的正则表达式有两个问题:
[]
)中,您只需引用字符类中的特殊字符,即连字符、方括号、反斜杠。以下调用对我有用:
Your regular expression has two problems:
[]
), you only need to quote characters that are special within character classes, i.e. hyphen, square bracket, backslash.The following call works for me:
目前,您的正则表达式将匹配字符串的开头 (
^
),然后恰好匹配方括号中的字符之一(即非法字符) ,然后是字符串结尾 ($
)。所以这可能不起作用,因为长度>的字符串1 很可能无法匹配正则表达式,因此被认为是正常的。
您可能不需要开始和结束锚点(
^
和$
)。如果删除这些,则正则表达式应该匹配输入文本中任何位置出现的括号字符之一,这就是您想要的。(根据具体的正则表达式方言,您可能在方括号内需要较少的反斜杠,但在任何情况下它们都不可能造成任何损害)。
As it stands at the moment, your regex will match the start of the string (
^
), then exactly one of the characters in the square brackets (i.e. the illegal characters), then then end of the string ($
).So this likely isn't working because a string of length > 1 will trivially fail to match the regex, and thus be considered OK.
You likely don't need the start and end anchors (the
^
and$
). If you remove these, then the regex should match one of the bracketed characters occurring anywhere on the input text, which is what you want.(Depending on the exact regex dialect, you may canonically need less backslashes within the square brackets, but they are unlikely to do any harm in any case).