如何使用正则表达式验证 iSCSI 目标名称?

发布于 2024-12-03 16:19:50 字数 533 浏览 1 评论 0原文

我正在尝试编写一个正则表达式来验证 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平安喜乐 2024-12-10 16:19:50

根据 RFC3270(以及 RFC1035),

/
   (?(DEFINE)
      (?<IQN_PAT>
         iqn
         \. 
         [0-9]{4}-[0-9]{2}
         \.
         (?&REV_SUBDOMAIN_PAT)
         (?: : .* )?
      )

      (?<EUI_PAT>
         eui
         \.
         [0-9A-Fa-f]{16}
      )

      (?<REV_SUBDOMAIN_PAT>
         (?&LABEL_PAT) (?: \. (?&LABEL_PAT) )*
      )

      (?<LABEL_PAT>
         [A-Za-z] (?: [A-Za-z0-9\-]* [A-Za-z0-9] )?
      )
   )

   ^ (?: (?&IQN_PAT) | (?&EUI_PAT) ) \z
/sx

尚不清楚 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),

/
   (?(DEFINE)
      (?<IQN_PAT>
         iqn
         \. 
         [0-9]{4}-[0-9]{2}
         \.
         (?&REV_SUBDOMAIN_PAT)
         (?: : .* )?
      )

      (?<EUI_PAT>
         eui
         \.
         [0-9A-Fa-f]{16}
      )

      (?<REV_SUBDOMAIN_PAT>
         (?&LABEL_PAT) (?: \. (?&LABEL_PAT) )*
      )

      (?<LABEL_PAT>
         [A-Za-z] (?: [A-Za-z0-9\-]* [A-Za-z0-9] )?
      )
   )

   ^ (?: (?&IQN_PAT) | (?&EUI_PAT) ) \z
/sx

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].)

等风也等你 2024-12-10 16:19:50

如果您只需要 : 之前的部分,则可以使用以下正则表达式:

if ($target_name =~ /^iqn\.(\d{4}-\d{2})\.([^:]+):/xmi) {
    my ($date, $reversed_domain_name) = ($1, $2);

Regexp [^:]+ 匹配 1 个或多个非 : 符号。即使域名格式不正确,它也会匹配。进一步的改进取决于您的目标:您是否只需要获取 iSCSI 名称的各个组成部分,还是需要验证其语法?


域名的大小有限制吗?

来自维基百科

完整域名总长度不得超过253

If you only need part before : then you can use following regexp:

if ($target_name =~ /^iqn\.(\d{4}-\d{2})\.([^:]+):/xmi) {
    my ($date, $reversed_domain_name) = ($1, $2);

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?


Is there a limit to how big a domain name can be?

From Wikipedia:

The full domain name may not exceed a total length of 253

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文