stringr:str\u匹配忽略表达式之间的文本

stringr:str\u匹配忽略表达式之间的文本,r,regex,stringr,R,Regex,Stringr,我有一个字符串,其中包含如下文本: Base_Text <- "John will sell Apple stock" 但是当文本变得更加详细时,我遇到了一些问题,例如: New_Text <- "John will sell, given the current situation of the market, all of his Apple stock" 有什么办法可以做到这一点吗?我提出了这个暴力解决方案,可以明显改进,我确信: l

我有一个字符串,其中包含如下文本:

Base_Text <- "John will sell Apple stock"
但是当文本变得更加详细时,我遇到了一些问题,例如:

New_Text <- "John will sell, given the current situation of the market, all of his Apple stock"

有什么办法可以做到这一点吗?

我提出了这个暴力解决方案,可以明显改进,我确信:

library(stringr)
New_str <- "John will sell, given the current situation of the market, all of his Apple stock"
start <- str_locate(New_str, pattern = "sell")[2]
end <- str_locate_all(New_str,
           pattern = "[:upper:][a-z]+")[[1]][2, 1]
paste(substr(New_str, 1, start + 1), substr(New_str, end - 1, str_count(New_str)), sep = "")
库(stringr)

新建\u str您可以使用以下正则表达式匹配以大写字母开头的“sell”后面的所有单词:
(?!.*\bsell\b)\b[a-Z][a-Z]+
。负向前看导致正则表达式的字符串指针移动到
'sell'
's'
'e'
之间的位置,因为此时满足了向前看。(我可能需要写
\\b
而不是
\b
,但R不是我的强项。)
“将出售\\b.\\b[:upper:][a-z]+股票”
?@Wiktor,奇怪的是你写的是
[:upper:][/code>,而不是
[a-z]
,然而
[a-z]
而不是
[:lower a:][/code>)等等,你不是想建立一个捕获组吗?@CarySwoveland从手机上复制/粘贴。
str_match(New_Text, "will sell [ignore everything in between] [:upper:][a-z]+ stock")
library(stringr)
New_str <- "John will sell, given the current situation of the market, all of his Apple stock"
start <- str_locate(New_str, pattern = "sell")[2]
end <- str_locate_all(New_str,
           pattern = "[:upper:][a-z]+")[[1]][2, 1]
paste(substr(New_str, 1, start + 1), substr(New_str, end - 1, str_count(New_str)), sep = "")