1

我需要检查一个字符串是否符合此规则:http ://www.w3.org/TR/widgets/#zip-rel-path

Zip-rel-path   = [locale-folder] *folder-name file-name /
                 [locale-folder] 1*folder-name
locale-folder  = %x6C %x6F %x63 %x61 %x6C %x65 %x73
                 "/" lang-tag "/"
folder-name    = file-name "/"
file-name      = 1*allowed-char
allowed-char   = safe-char / zip-UTF8-char
zip-UTF8-char  = UTF8-2 / UTF8-3 / UTF8-4
safe-char      = ALPHA  / DIGIT / SP  / "$" / "%" / 
                 "'"    / "-"   / "_" / "@" / "~" /
                 "("    / ")"   / "&" / "+" / "," /
                 "="    / "["   / "]" / "."
UTF8-2         = %xC2-DF UTF8-tail
UTF8-3         = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
                 %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
UTF8-4         = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
                 %xF4 %x80-8F 2( UTF8-tail )
UTF8-tail      = %x80-BF
lang-tag       = primary-subtag *( "-" subtag )
primary-subtag = 1*8low-alphasubtag         = 1*8(alphanum)
alphanum       = low-alpha  / DIGITlow-alpha      = %x61-7a

完全符合上述规则的代码示例会有所帮助,我不熟悉 ABNF。我不需要解析 ABNF 的方法,我只需要由习惯或理解 ABNF 的人手动将上述规则翻译成带有正则表达式或任何其他方式的 python 代码。实际上,只需输入一个字符串并根据上述规则验证它最终作为一个输入字符串并在规则匹配与否时返回 true 或 false 的函数。所以用一个问题的形式来表达:这在 python 中会如何实现?

我从 UTF8 文档中看到,上述规则的大部分内容只是检查字符串是否为 utf8: https ://www.rfc-editor.org/rfc/rfc3629

UTF8-char   = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
   UTF8-1      = %x00-7F
   UTF8-2      = %xC2-DF UTF8-tail
   UTF8-3      = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
                 %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
   UTF8-4      = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
                 %xF4 %x80-8F 2( UTF8-tail )
   UTF8-tail   = %x80-BF  
4

2 回答 2

2

您可能应该尝试pyparsing。这是一个来自 pyparsing 网站的快速示例,您可以轻松修改以适合您的目的。

于 2012-08-20T16:31:59.123 回答
1

我试图为你写一个解析器。

我同意批量是对 UTF-8 的测试,如果您已经在字符串中具有值,这是多余的(UTF-8 是文件系统上的编码,unicode 是有效UTF-8 的内部表示)。这确实极大地简化了事情。

据我了解,BNF 说:

  • locale-folder(可选)是字符串 'locale/' 后跟lang-tag
  • lang-tag的形式为 'en'、'en-us'、'en-123'、'en-us-1' 等等:
    • 至少一个记号,用'-'字符隔开
    • 每个标记为 1 到 8 个字符
    • 第一个标记可能只有小写字母
    • 以下标记是小写字母和数字的混合
  • 在可选语言环境之后,您可以拥有:
    • 单个文件名
    • 路径(一系列以“/”分隔的文件夹名称)或
    • 后跟文件名的路径
  • 文件夹名文件名是一种 unicode。每个字符要么
    • AZ、az、0-9 或
    • “$%'-_@~()&+,=[]”中的任何一个。
    • u007F以上的任何字符(UTF8 二、三和四字节字符)

也就是说,这是一个简单的实现(为了调试,它捕获解析的输出。我这样做是为了调试,但如果你不需要它,请随意删除它)。路径中的错误会导致 ZipRelPath 构造函数引发 ValueError:

import re

class ZipRelPath:
    FILE_NAME_RE = re.compile(u"^[a-zA-Z0-9 \$\%\'\-_@~\(\)&+,=\[\]\.\u0080-\uFFFF]+$")
    LANG_TAG_RE  = re.compile("^[a-z]{1,8}(\-[a-z0-9]{1,8})*$")
    LOCALES      = "locales/"

    def __init__(self, path):
        self.path = path
        self.lang_tag = None
        self.folders = []
        self.file_name = None

        self._parse_locales()
        self._parse_folders()

    def _parse_locales(self):
        """Consumes any leading 'locales' and lang-tag"""
        if self.path.startswith(ZipRelPath.LOCALES):
            self.path = self.path[len(ZipRelPath.LOCALES):]
            self._parse_lang_tag()

    def _parse_lang_tag(self):
        """Parses, consumes and validates the lang-tag"""
        self.lang_tag, _, self.path = self.path.partition("/")
        if not self.path:
            raise ValueError("lang-tag missing closing /")
        if not ZipRelPath.LANG_TAG_RE.match(self.lang_tag):
            raise ValueError(u"'%s' is not a valid language tag" % self.lang_tag)

    def _parse_folders(self):
        """Handles the folders and file-name after the locale"""
        while (self.path):
            self._parse_folder_or_file()

        if not self.folders and not self.file_name:
            raise ValueError("Missing folder or file name")

    def _parse_folder_or_file(self):
        """Each call consumes a single path entry, validating it"""
        folder_or_file, _, self.path = self.path.partition("/")
        if not ZipRelPath.FILE_NAME_RE.match(folder_or_file):
            raise ValueError(u"'%s' is not a valid file or folder name" % folder_or_file)
        if self.path:
            self.folders.append(folder_or_file)
        else:
            self.file_name = folder_or_file

    def __unicode__(self):
        return u"ZipRelPath [lang-tag: %s, folders: %s, file_name: %s" % (self.lang_tag, self.folders, self.file_name)

还有一组简短的测试:

GOOD = [
    "$%'-_@~()&+,=[].txt9",
    "my/path/to/file.txt",
    "locales/en/file.txt",
    "locales/en-us/file.txt",
    "locales/en-us-abc123-xyz/file.txt",
    "locales/abcdefgh-12345678/file.txt",
    "locales/en/my/path/to/file.txt",
    u"my\u00A5\u0160\u039E\u04FE\u069E\u0BCC\uFFFD/path/to/file.txt"
]
BAD   = [
    "",
    "/starts/with/slash",
    "bad^file",
    "locales//bad/locale",
    "locales/en123/bad/locale",
    "locales/EN/bad/locale",
    "locales/en-US/bad/locale",
    ]

for path in GOOD:
    print unicode(ZipRelPath(path))

for path in BAD:
    try:
        zip = ZipRelPath(path)
        raise Exception("Illegal path {0} was accepted by {1}".format(path, zip))
    except ValueError as exception:
        print "Incorrect path '{0}' fails with: {1}".format(path, exception)

产生:

ZipRelPath [lang-tag: None, folders: [], file_name: $%'-_@~()&+,=[].txt9
ZipRelPath [lang-tag: None, folders: ['my', 'path', 'to'], file_name: file.txt
ZipRelPath [lang-tag: en, folders: [], file_name: file.txt
ZipRelPath [lang-tag: en-us, folders: [], file_name: file.txt
ZipRelPath [lang-tag: en-us-abc123-xyz, folders: [], file_name: file.txt
ZipRelPath [lang-tag: abcdefgh-12345678, folders: [], file_name: file.txt
ZipRelPath [lang-tag: en, folders: ['my', 'path', 'to'], file_name: file.txt
ZipRelPath [lang-tag: None, folders: [u'my\xa5\u0160\u039e\u04fe\u069e\u0bcc\ufffd', u'path', u'to'], file_name: file.txt
Incorrect path '' fails with: Missing folder or file name
Incorrect path '/starts/with/slash' fails with: '' is not a valid file or folder name
Incorrect path 'bad^file' fails with: 'bad^file' is not a valid file or folder name
Incorrect path 'locales//bad/locale' fails with: '' is not a valid language tag
Incorrect path 'locales/en123/bad/locale' fails with: 'en123' is not a valid language tag
Incorrect path 'locales/EN/bad/locale' fails with: 'EN' is not a valid language tag
Incorrect path 'locales/en-US/bad/locale' fails with: 'en-US' is not a valid language tag

如果您的任何测试用例失败,请告诉我,我会看看是否可以修复它。

于 2012-08-29T09:31:18.603 回答