Python 相当于 ruby​​ 的 StringScanner?

2024-01-29

是否有与 ruby​​ 等效的 python 类StringScanner 类 http://ruby-doc.org/core/classes/StringScanner.html?我可以将一些东西组合在一起,但如果这已经存在,我不想重新发明轮子。


有趣的是,有一个无证的Scanner http://code.activestate.com/recipes/457664/类在re http://docs.python.org/library/re.html module:

import re

def s_ident(scanner, token): return token
def s_operator(scanner, token): return "op%s" % token
def s_float(scanner, token): return float(token)
def s_int(scanner, token): return int(token)

scanner = re.Scanner([
    (r"[a-zA-Z_]\w*", s_ident),
    (r"\d+\.\d*", s_float),
    (r"\d+", s_int),
    (r"=|\+|-|\*|/", s_operator),
    (r"\s+", None),
    ])

print scanner.scan("sum = 3*foo + 312.50 + bar")

讨论 http://mail.python.org/pipermail/python-dev/2003-April/035070.html看起来它是作为实验代码/其他人的起点留下的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 相当于 ruby​​ 的 StringScanner? 的相关文章