Python RegExp,获取不带certan单词的组

2024-09-28 05:21:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有如下文字:

qwerqwer asdf foo bar
%abc anchor

one
%abc anchor

one thow foo bar
%abc anchor

one two three
%abc anchor

现在我有了regexp(.+)\n\%\w+\sanchor,但它给了我('qwerqwer asdf foo bar', 'one thow foo bar', 'one', 'one two three')我想从结果字符串foo bar中排除,并得到结果('qwerqwer asdf', 'one thow', 'one', 'one two three')

也许有人解决了类似的问题


Tags: 字符串foobaronethree文字abcanchor
1条回答
网友
1楼 · 发布于 2024-09-28 05:21:53
(.+?)(?: foo bar)?\n\%\w+\sanchor

您需要使用不情愿的修饰符(+?),这样它就不会吃foo bar,并且在行的末尾添加一个可选的foo bar

https://regex101.com/r/fU2jA5/1

相关问题 更多 >

    热门问题