我想从一个文本文件中提取与几个类别相关的数据(除了类别A和B)。子字符串的格式应该类似于
some text preceding Category C This is some text I'm aware of belonging to the categories为了处理上面的问题,不包括A和B类数据,我有一个简单的负后面正则表达式
(?<!Category A )(?<!Category B )This is some text I'm aware of然而,我也有一些有限的例子,在文本中的Category A/B后面会有几个字符(最多5个)。例如:
some text Category A 1. This is some text I'm aware of belonging to the categories因此,我尝试将regex改为:
(?<!Category A.{5})(?<!Category B.{5})This is some text I'm aware of它在CatA/B之后准确地工作了5个字符,但它不允许我将{5}更改为{0,5}并抱怨:
量词不固定
我怎么才能让这个起作用?
发布于 2021-09-19 11:41:43
发布于 2021-09-19 14:21:11
使用Python,如果要匹配除A和B以外的大写字符之外的类别,则可以匹配can,后面跟着0-5个字符,并捕获捕获组中的文本。
\bCategory [C-Z].{0,5}\b(This is some text I'm aware of)\bhttps://stackoverflow.com/questions/69242881
复制相似问题