gpt4 book ai didi

ruby - 否定使用前面的\K组

转载 作者:数据小太阳 更新时间:2023-10-29 07:52:17 27 4
gpt4 key购买 nike

我可以否定使用前面的\K 组吗?我可以匹配 faxbar funbar bobarbar 中的 bar 并省略 bar在 foobar foobar foooooobar 里面? https://regex101.com/r/qR9kD4/5

正则表达式

fo*\Kbar

字符串:(- 和期望的结果)

foobar       - no match
fobar - no match
foooooobar - no match
faxbar - match 'bar'
funbar - match 'bar'
bobar - match 'bar'
bar - match 'bar'
dontmineidontwanttfooooobematchedaatall - no match

基本上反转我当前的匹配(除了 dontmineme...)。希望我只需要添加一个 ! 之类的东西!

最佳答案

您可以将 \K 与 Negative Lookahead 断言一起使用:

\b(?!fo+)\w*\Kbar

Demo

一个简单的解决方案是将您想要忽略的内容放在交替运算符的左侧,并将您想要匹配的内容放在交替运算符右侧的捕获组中。

fo+bar|\w*(bar)

关于ruby - 否定使用前面的\K组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31737050/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com