pyparsing 匹配不包含cfg_file_name字符串的其他字符串
时间: 2024-02-03 21:02:25 浏览: 26
你可以使用pyparsing的~操作符来实现这个功能。具体来说,你可以将字符串“cfg_file_name”解析为Literal对象,并使用~操作符将其取反。这将创建一个NotAny对象,该对象将匹配不包含“cfg_file_name”的任何字符串。以下是一个示例代码:
``` python
from pyparsing import Literal, NotAny, Word, alphas
cfg_file_name = Literal("cfg_file_name")
other_string = Word(alphas)
# 匹配不包含 "cfg_file_name" 的 other_string
pattern = NotAny(cfg_file_name) + other_string
# 测试代码
test_string_1 = "cfg_file_name"
test_string_2 = "other_string"
print(pattern.parseString(test_string_1)) # Raises ParseException
print(pattern.parseString(test_string_2)) # Returns ['other_string']
```
在上面的代码中,我们首先定义了两个模式:cfg_file_name和other_string。然后,我们使用NotAny操作符将cfg_file_name取反,并将其与other_string组合在一起,以匹配不包含cfg_file_name的任何字符串。最后,我们使用parseString方法测试了两个字符串,一个包含cfg_file_name,另一个不包含。