如何使用python检查字符串对某些特定的ABNF规则?

时间:2012-08-20 16:24:30

标签: python regex python-2.7

如果符合此规则,我需要检查字符串: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: http://tools.ietf.org/html/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  

2 个答案:

答案 0 :(得分:2)

您应该尝试pyparsing。这是来自pyparsing网站的quick example,您可以轻松修改以满足您的目的。

答案 1 :(得分: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。每个角色都是
    • A-Z,a-z,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

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