我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用pyparsing.CaselessLiteral()。
def search_words_pyparsing_set(text, keywords): words_list = set(re.findall(r"[\w]+", text)) for key in keywords: count = 0 words = key.split() for word in words: if CaselessLiteral(word).searchString(words_list): count += 1 if count == len(words): return True return False
def search_words_pyparsing_list(text, keywords): words_list = re.findall(r"[\w]+", text) for key in keywords: count = 0 words = key.split() for word in words: if CaselessLiteral(word).searchString(words_list): count += 1 if count == len(words): return True return False
def number_parser(): point = pp.Literal(".") e = pp.CaselessLiteral("e") plusorminus = pp.Literal("+") ^ pp.Literal("-") num = pp.Word(pp.nums) dec = pp.Combine(num + pp.Optional(point + pp.Optional(num)) + pp.Optional(e + pp.Optional(plusorminus) + num)) ^\ pp.Combine(point + pp.Optional(num) + pp.Optional(e + pp.Optional(plusorminus) + num)) bin = pp.Combine(pp.Literal("0") + pp.CaselessLiteral("b") + pp.Word("01")) hex = pp.Combine(pp.Literal("0") + pp.CaselessLiteral("x") + pp.Word(pp.hexnums)) oct = pp.Combine(pp.Literal("0") + pp.Optional(pp.CaselessLiteral("o")) + pp.Word("01234567")) return dec ^ bin ^ hex ^ oct
def parseTerms(): """ expop :: '^' multop :: '*' | '/' addop :: '+' | '-' integer :: ['+' | '-'] '0'..'9'+ atom :: PI | E | real | fn '(' expr ')' | '(' expr ')' factor :: atom [ expop factor ]* term :: factor [ multop factor ]* expr :: term [ addop term ]* """ global terms if not terms: point = Literal( "." ) e = CaselessLiteral( "E" ) fnumber = Combine( Word( "+-"+nums, nums ) + Optional( point + Optional( Word( nums ) ) ) + Optional( e + Word( "+-"+nums, nums ) ) ) ident = Word(alphas, alphas+nums+"_$") plus = Literal( "+" ) minus = Literal( "-" ) mult = Literal( "*" ) div = Literal( "/" ) lpar = Literal( "(" ).suppress() rpar = Literal( ")" ).suppress() addop = plus | minus multop = mult | div expop = Literal( "^" ) pi = CaselessLiteral( "PI" ) expr = Forward() atom = (Optional("-") + ( pi | e | fnumber | ident + lpar + expr + rpar ).setParseAction( pushFirst ) | ( lpar + expr.suppress() + rpar )).setParseAction(pushUMinus) # by defining exponentiation as "atom [ ^ factor ]..." instead of "atom [ ^ atom ]...", we get right-to-left exponents, instead of left-to-righ # that is, 2^3^2 = 2^(3^2), not (2^3)^2. factor = Forward() factor << atom + ZeroOrMore( ( expop + factor ).setParseAction( pushFirst ) ) term = factor + ZeroOrMore( ( multop + factor ).setParseAction( pushFirst ) ) expr << term + ZeroOrMore( ( addop + term ).setParseAction( pushFirst ) ) terms = expr return terms