gpt4 book ai didi

r - 如何在后视中使用交替

转载 作者:行者123 更新时间:2023-12-04 12:04:09 25 4
gpt4 key购买 nike

目的:

我想将句子与单词“no”匹配,但前提是在 r 中“no”之前没有“with”或“there is”或“there are”。

输入:

The ground was rocky with no cracks in it
No diggedy, no doubt
Understandably, there is no way an elephant can be green

预期输出:
The ground was rocky with no cracks in it
Understandably, there is no way an elephant can be green

尝试:
gsub(".*(?:((?<!with )|(?<!there is )|(?<!there are ))\\bno\\b(?![?:A-Za-z])|([?:]\\s*N?![A-Za-z])).*\\R*", "", input_string, perl=TRUE, ignore.case=TRUE)

问题:

否定的lookbehind似乎被忽略了,所以所有的句子都被替换了。问题是在lookbehind语句中使用交替吗?

最佳答案

你只需要有一个正则表达式交替字符。想法是匹配并捕获所有可能的“否”句子并匹配所有剩余的句子。然后用 \\1 替换所有匹配的字符即,来自第一个捕获组的字符。

gsub("(?i)(.*(with|there (?:is|are)) no\\b.*)|.*", "\\1" ,string, perl=T)

DEMO

例子:
x <- "The ground was rocky with no cracks in it\nNo diggedy, no doubt\nUnderstandably, there is no way an elephant can be green"
gsub("(?i)(.*(with|there (?:is|are)) no\\b.*\\n?)|.*\\n?", "\\1" ,x, perl=T)
# [1] "The ground was rocky with no cracks in it\nUnderstandably, there is no way an elephant can be green"

关于r - 如何在后视中使用交替,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47010713/

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