标签:inf class int end col idt 错误 exp png
1.Word(token)
用于匹配由允许的字符集组成的单词,常见的错误是使用特定字符串Word("expr")匹配"expr"
- L {alphas} 字母
- L {nums} 数字
- L {alphanums} 数字字母混合
2.Suppress
忽略表达式中内容
import pyparsing as pp source = "a , b, c, d" wd = pp.Word(pp.alphas) wd_list = wd + pp.ZeroOrMore(‘,‘+ wd) print wd_list.parseString(source) # result [‘a‘, ‘,‘, ‘b‘, ‘,‘, ‘c‘, ‘,‘, ‘d‘] # ZeroOrMore wd_list = wd +pp.ZeroOrMore(pp.Suppress(‘,‘)+wd) print wd_list.parseString(source) # [‘a‘, ‘b‘, ‘c‘, ‘d‘]
3. Group
使用group将返回的结果,加上[] 结构
from pyparsing import * wd = Word(alphas) comma = Literal(",") greetee = OneOrMore(wd) end = oneOf("! ?") greeting = wd + comma + greetee + end # result::[‘Hello‘, ‘,‘, ‘World‘, ‘!‘] print greeting.parseString("Hello,World!")
wd = Group(Word(alphas)) comma = Literal(",") greetee = OneOrMore(wd) end = oneOf("! ?") greeting = wd + comma + greetee + end # [[‘Hello‘], ‘,‘, [‘World‘], ‘!‘] print greeting.parseString("Hello,World!")
from pyparsing import * wd = Word(alphas) comma = Literal(",").suppress() greetee = OneOrMore(wd) end = oneOf("! ?").suppress() greeting = wd + comma + greetee + end #[‘Hello‘, ‘World‘] print greeting.parseString("Hello,World!")
标签:inf class int end col idt 错误 exp png
原文地址:https://www.cnblogs.com/CaesarLinsa/p/10714056.html