gpt4 book ai didi

regex - 替换 R 中第一次出现的 ":",但不替换第二次出现的

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

为了能够处理,我想替换字符串中第一次出现的 : (这是我的标记,表示演讲开始)。

text <- c("Mr. Mark Francois (Rayleigh) (Con): If the scheme was so poorly targeted, why were the Government about to roll it out to employees in the Department of Trade and Industry and the Department for Work and Pensions on the very day the Treasury scrapped it? The CBI and the TUC have endorsed the scheme, which has helped 500,000 people and their families to improve their computer skills. When the Chancellor announced the original concession, he told the  Daily Record:", "Even at this eleventh hour, will the Government recognise that this is a poor decision, taken by an analogue Chancellor who is stuck in the past and reversing?", "Dawn Primarolo: The hon. Gentleman answers his own question, as the US does not have similar schemes. He is right to address the question of how we give people in the greatest need access to computer technology, but the Low Pay Commission\u0092s 2005 report showed that that was not happening under the scheme. Why should the Government spend £200 million on a poorly targeted scheme? It was being abused and was not delivering, so the Government have refocused to ensure that the objective is achieved.")

我可以使用

找到第一个 :的位置
lapply(gregexpr("\\:", text), head, 1)

[[1]]
[1] 35

[[2]]
[1] -1

[[3]]
[1] 15

但是,我无法在 text 中替换它(例如,用 |)。

最佳答案

我们可以使用sub,因为它只匹配第一次出现的模式:,然后我们将其替换为|

sub(':', '|', text)

关于regex - 替换 R 中第一次出现的 ":",但不替换第二次出现的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34269466/

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