如何使用正则表达式验证 iSCSI 目标名称?
我正在尝试编写一个正则表达式来验证 iSCSI 限定名称。限定名称的示例是 iqn.2011-08.com.example:storage
这是一个最小的示例,我见过其他更扩展的示例。
到目前为止,我必须验证以下内容:
print "Enter a new target name: ";
my $target_name = <STDIN>;
chomp $target_name;
if ($target_name =~ /^iqn\.\d{4}-\d{2}/xmi) {
print GREEN . "Target name is valid!" . RESET . "\n";
} else {
print RED . "Target name is not valid!" . RESET . "\n";
}
我怎样才能将其扩展到 :
之前的其余部分,我不会在 :
之后进行解析,因为它是一个描述标签。
域名的大小有限制吗?
I am trying to craft a regexp to validate iSCSI qualified names. An example of a qualified name is iqn.2011-08.com.example:storage
This is example is minimal, I have seen other examples that are more extended.
So far what I have to validate off of it this:
print "Enter a new target name: ";
my $target_name = <STDIN>;
chomp $target_name;
if ($target_name =~ /^iqn\.\d{4}-\d{2}/xmi) {
print GREEN . "Target name is valid!" . RESET . "\n";
} else {
print RED . "Target name is not valid!" . RESET . "\n";
}
How can I extend that to work with rest up to the :
I am not going to parse after the :
becuase it is a description tag.
Is there a limit to how big a domain name can be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 RFC3270(以及 RFC1035),
尚不清楚 eui 名称是否接受小写十六进制数字。我认为允许他们这样做更安全。
如果你浓缩上面的内容,你会得到
/^(?:iqn\.[0-9]{4}-[0-9]{2}(?:\.[A-Za-z](?:[A-Za-z0- 9\-]*[A-Za-z0-9])?)+(?::.*)?|eui\.[0-9A-Fa-f]{16})\z/s
。(顺便说一句,你使用
/m
是错误的,你使用/i
是错误的,而\d
可以匹配的远远多于允许[0-9]
。)According to RFC3270 (and in turn RFC1035),
It's not clear if the eui names accept lowercase hex digits or not. I figured it was safer to allow them.
If you condense the above, you get
/^(?:iqn\.[0-9]{4}-[0-9]{2}(?:\.[A-Za-z](?:[A-Za-z0-9\-]*[A-Za-z0-9])?)+(?::.*)?|eui\.[0-9A-Fa-f]{16})\z/s
.(By the way, your use
/m
is wrong, your use of/i
is wrong, and\d
can match far more than the allowed[0-9]
.)如果您只需要
:
之前的部分,则可以使用以下正则表达式:Regexp
[^:]+
匹配 1 个或多个非:
符号。即使域名格式不正确,它也会匹配。进一步的改进取决于您的目标:您是否只需要获取 iSCSI 名称的各个组成部分,还是需要验证其语法?来自维基百科:
If you only need part before
:
then you can use following regexp:Regexp
[^:]+
matches to 1 or more non-:
symbols. It will match even if domain name is not well formed. Further improvements depends on your goal: do you need just get individual components of iSCSI name or do you need to validate its syntax?From Wikipedia: