我试图寻找那些不会立即出现在the
前面的单词
执行正向查找以获取关键字“the”(?<=the\W)
后面的单词。然而,我无法捕捉“人”和“那”,因为上述逻辑不适用于这些案例
我无法处理前后没有关键字“the”的单词(例如,句子中的“that”和“people”)
p = re.compile(r'(?<=the\W)\w+')
m = p.findall('the part of the fair that attracts the most people is the fireworks')
print(m)
我得到的电流输出是
'part','fair','most','fireworks'.
编辑:
感谢您在下面提供的所有帮助。在评论中使用以下建议,成功地更新了我的代码
p = re.compile(r"\b(?!the)(\w+)(\W\w+\Wthe)?")
m = p.findall('the part of the fair that attracts the most people is the fireworks')
这使我更接近我需要得到的输出
更新输出:
[('part', ' of the'), ('fair', ''),
('that', ' attracts the'), ('most', ''),
('people', ' is the'), ('fireworks', '')]
我只需要字符串(‘部分’、‘公平’、‘那个’、‘大多数’、‘人’、‘焰火’)。 有什么建议吗
使用正则表达式:
输出:
请注意,下面的代码不使用
re
输出
试试这个:
输出:
相关问题 更多 >
编程相关推荐