前言

  • 技术栈
python      3.11.8
pyparsing   3.2.1
regex       2024.11.6

测试案例

  • 测试代码
# encoding: utf-8
# author: qbit
# date: 2025-02-05
# summary: 使用 pyparsing 和 sympy 化简与或非逻辑表达式

import pyparsing as pp
import regex
# greet = pp.Word(pp.alphas) + "," + pp.Regex(r'\p{Han}+') + "!"
# 上面一句会报错,因为 pyparsing 默认使用的内置 re 库不支持 Unicode 正则
greet = pp.Word(pp.alphas) + "," + pp.Regex(regex.compile(r'\p{Han}+')) + "!"
hello = "Hello, 中国!"
print(hello, "->", greet.parseString(hello))
  • 测试输出
Hello, 中国! -> ['Hello', ',', '中国', '!']

相关阅读

本文出自 qbit snap

qbit
271 声望279 粉丝