码迷,mamicode.com
首页 > 其他好文 > 详细

pyparsing

时间:2019-04-16 01:11:40      阅读:167      评论:0      收藏:0      [点我收藏+]

标签: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!")

 

 

pyparsing

标签:inf   class   int   end   col   idt   错误   exp   png   

原文地址:https://www.cnblogs.com/CaesarLinsa/p/10714056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!