匹配 VMS 文件名的正确正则表达式模式是什么?

发布于 2024-10-08 04:15:13 字数 515 浏览 10 评论 0原文

文档位于 http://h71000.www7.hp.com/ doc/731final/documentation/pdf/ovms_731_file_app.pdf (第 5-1 节)表示文件名应如下所示:

node::device:[root.][directory-name]filename.type;version

其中大多数是可选的(如节点、设备、版本) - 不确定哪些以及如何在正则表达式中正确编写此内容(包括目录名称):

DISK1:[MYROOT.][MYDIR]FILE.DAT

DISK1:[MYDIR]FILE.DAT

[MYDIR]FILE.DAT

FILE.DAT;10

NODE::DISK5:[REMOTE.ACCESS]FILE.DAT

The documentation at http://h71000.www7.hp.com/doc/731final/documentation/pdf/ovms_731_file_app.pdf (section 5-1) says the filename should look like this:

node::device:[root.][directory-name]filename.type;version

Most of them are optional (like node, device, version) - not sure which ones and how to correctly write this in a regexp, (including the directory name):

DISK1:[MYROOT.][MYDIR]FILE.DAT

DISK1:[MYDIR]FILE.DAT

[MYDIR]FILE.DAT

FILE.DAT;10

NODE::DISK5:[REMOTE.ACCESS]FILE.DAT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

财迷小姐 2024-10-15 04:15:13

请参阅 VMS::Filespec Perl 模块的文档和源代码。

See the documentation and source for the VMS::Filespec Perl module.

帅哥哥的热头脑 2024-10-15 04:15:13

从维基百科来看,完整的形式实际上比这要复杂一些:

NODE"accountname password"::device:[directory.subdirectory]filename.type;ver

这花了一段时间,但这里是一个应该接受所有有效变体并将组件放入捕获组的表达式。

(?:(?:(?:([^\s:\[\]]+)(?:"([^\s"]+) ([^\s"]+)")?::)?([^\s:\[\]]+):)?\[([^\s:\[\]]+)\])?([^\s:\[\]\.]+)(\.[^\s:\[\];]+)?(;\d+)?

另外,据我所知,您的示例

DISK1:[MYROOT.][MYDIR]FILE.DAT

不是有效的名称。我相信只允许使用一对括号。我希望这有帮助!

From wikipedia, the full form is actually a bit more than that:

NODE"accountname password"::device:[directory.subdirectory]filename.type;ver

This one took a while, but here is an expression that should accept all valid variations, and place the components into capture groups.

(?:(?:(?:([^\s:\[\]]+)(?:"([^\s"]+) ([^\s"]+)")?::)?([^\s:\[\]]+):)?\[([^\s:\[\]]+)\])?([^\s:\[\]\.]+)(\.[^\s:\[\];]+)?(;\d+)?

Also, from what I can tell, your example of

DISK1:[MYROOT.][MYDIR]FILE.DAT

is not a valid name. I believe only one pair of brackets are allowed. I hope this helps!

卷耳 2024-10-15 04:15:13

您可能会为此想出一个复杂的正则表达式,但是如果您从左到右剥离每个部分(如果存在),那么阅读代码会容易得多。以下是一些执行此操作的 Python 代码:

lines = ["DISK1:[MYROOT.][MYDIR]FILE.DAT", "DISK1:[MYDIR]FILE.DAT", "[MYDIR]FILE.DAT", "FILE.DAT;10", "NODE::DISK5:[REMOTE.ACCESS]FILE.DAT"]
node_re = "(\w+)::"
device_re = "(\w+):"
root_re = "\[(\w+)\.]"
dir_re = "\[(\w+)]"
file_re = "(\w+)\."
type_re = "(\w+)"
version_re = ";(.*)"
re_dict = {"node": node_re, "device": device_re, "root": root_re, "directory": dir_re, "file": file_re, "type": type_re, "version": version_re}
order = ["node", "device", "root", "directory", "file", "type", "version"]
for line in lines:
    i = 0
    print line
    for item in order:
        m = re.search(re_dict[item], line[i:])
        if m is not None:
            print "  " + item + ": " + m.group(1)
            i += len(m.group(0))

输出为

DISK1:[MYROOT.][MYDIR]FILE.DAT
  device: DISK1
  root: MYROOT
  directory: MYDIR
  file: FILE
  type: DAT
DISK1:[MYDIR]FILE.DAT
  device: DISK1
  directory: MYDIR
  file: FILE
  type: DAT
[MYDIR]FILE.DAT
  directory: MYDIR
  file: FILE
  type: DAT
FILE.DAT;10
  file: FILE
  type: DAT
  version: 10
NODE::DISK5:[REMOTE.ACCESS]FILE.DAT
  node: NODE
  device: DISK5
  directory: REMOTE.ACCESS
  file: FILE
  type: DAT

You could probably come up with a single complicated regex for this, but it will be much easier to read your code if you work your way from left to right stripping off each section if it is there. The following is some Python code that does just that:

lines = ["DISK1:[MYROOT.][MYDIR]FILE.DAT", "DISK1:[MYDIR]FILE.DAT", "[MYDIR]FILE.DAT", "FILE.DAT;10", "NODE::DISK5:[REMOTE.ACCESS]FILE.DAT"]
node_re = "(\w+)::"
device_re = "(\w+):"
root_re = "\[(\w+)\.]"
dir_re = "\[(\w+)]"
file_re = "(\w+)\."
type_re = "(\w+)"
version_re = ";(.*)"
re_dict = {"node": node_re, "device": device_re, "root": root_re, "directory": dir_re, "file": file_re, "type": type_re, "version": version_re}
order = ["node", "device", "root", "directory", "file", "type", "version"]
for line in lines:
    i = 0
    print line
    for item in order:
        m = re.search(re_dict[item], line[i:])
        if m is not None:
            print "  " + item + ": " + m.group(1)
            i += len(m.group(0))

and the output is

DISK1:[MYROOT.][MYDIR]FILE.DAT
  device: DISK1
  root: MYROOT
  directory: MYDIR
  file: FILE
  type: DAT
DISK1:[MYDIR]FILE.DAT
  device: DISK1
  directory: MYDIR
  file: FILE
  type: DAT
[MYDIR]FILE.DAT
  directory: MYDIR
  file: FILE
  type: DAT
FILE.DAT;10
  file: FILE
  type: DAT
  version: 10
NODE::DISK5:[REMOTE.ACCESS]FILE.DAT
  node: NODE
  device: DISK5
  directory: REMOTE.ACCESS
  file: FILE
  type: DAT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文